1084

Problem with URL Redirection.

Showing 1 to 10 of 11 comments.

Comments for “Problem with URL Redirection.”
 

Posted by siri on Wednesday 17th April 2024 at 10:24 GMT

Hi,
Generating URL - https://mywebsite.com/blog/blog_detail/enhancing-business-efficiency-the-ease-of-use-in-ap-automation
But desired URL - https://mywebsite.com/blog/enhancing-business-efficiency-the-ease-of-use-in-ap-automation
I want to remove blog_detail from the URL. The below line I have added in custom_routes.php
'blog/(:any)' => 'blog/blog_detail/$1'
But this is not working. I am getting 404 Error : Page Not Found Error
Can some one please help me
Best Regards,
Siri K
Early Adopter

siri

User Level: Early Adopter

Date Joined: 19/07/2019

Posted by Dom on Wednesday 17th April 2024 at 11:24 GMT

This is a pure guess on my part so in truth I don't expect it will work but have you tried;

'blog/' => 'blog/blog_detail/'
Level One Member

Dom

User Level: Level One Member

Date Joined: 12/01/2024

Posted by siri on Wednesday 17th April 2024 at 11:47 GMT

Hi,
Thanks for your reply .Yes, I tried.. this is also not working.. because I am getting blog title also as part of the URL like this
https://mywebsite.com/blog/blog_detail/enhancing-business-efficiency-the-ease-of-use-in-ap-automation
Early Adopter

siri

User Level: Early Adopter

Date Joined: 19/07/2019

Posted by DaFa on Wednesday 17th April 2024 at 14:03 GMT

Hi Siri,
It's working for me. Can you share your custom_routing.php file?

I tested with a module created using the VS Code extension, called 'blog'. Then created a method with 'tgm' +tab called 'blog_detail' (using Trongate Version: 1.3.3049)
<?php
class Blog extends Trongate {

    function index() {
        $data['view_module'] = 'blog';
        $this->view('display', $data);
    }
    function blog_detail() {

        $data['view_module'] = 'blog';
        $data['view_file'] = 'display';
        $this->template('public', $data);
    }
}
then my custom_routing.php file looks like this
<?php
$routes = [
    'tg-admin' => 'trongate_administrators/login',
    'trongate-pages' => 'trongate_pages/index',
    'tg-admin/submit_login' => 'trongate_administrators/submit_login',
    'blog/(:any)' => 'blog/blog_detail/$1'
];

define('CUSTOM_ROUTES', $routes);
and the URL that works fine for me
http://localhost/help_bar/blog/enhancing-business-efficiency-the-ease-of-use-in-ap-automation
Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

Posted by djnordeen on Wednesday 17th April 2024 at 16:27 GMT

Another question.
Is blog_detail a sub module. If so it should be
blog-detail

Dan
Early Adopter

djnordeen

User Level: Early Adopter

Date Joined: 20/08/2021

Posted by DaFa on Thursday 18th April 2024 at 00:21 GMT

Hi Dan,

No, 'blog_detail' is method (function) inside of the blog module - as seen above in my sample code
Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

Posted by siri on Thursday 18th April 2024 at 01:52 GMT

Hi Simon,
Thanks for your reply. This site is related to blog. I am passing the blog_id also to the url . So based on the blog_id i need to display the blog details.
But based on client requirement , we should not display blog_id in the url section. So the desired url will looks like this.
https://mywebsite.com/blog/comprehensive-salesforce-solutions

Here is the href link when user click on the blog title
<a href="<?php echo BASE_URL?>blog/<?php echo Blog::generateSlug($blog_categories[$i]['blog_title'])?>/<?php echo $blog_id; ?>"link</a>

blog/blog_detail function
function blog_detail($title,$blog_id) {
	//echo "segment".$seg = segment(3);//post('blog_id');
	echo $sel = "SELECT * from blog_details where id = '".$blog_id."'";
	$sel_res = $this->model->query($sel, 'array');
	$data['blog_detail'] = $sel_res;
	$data['view_module'] = 'blog';
	$data['view_file'] = 'blog_detail';
	$this->template('arc_inner', $data);
}

with the above href link I am getting 404 error. If i remove id from the url for example url looks like this
https://mywebsite.com/blog/comprehensive-salesforce-solutions , I am getting empty page.
Here is the custom_routing.php
<?php
$routes = [
    'tg-admin' => 'trongate_administrators/login',
    'tg-admin/submit_login' => 'trongate_administrators/submit_login',
    'blog/(:any)' => 'blog/blog_detail/$1'
];
define('CUSTOM_ROUTES', $routes);

Is there any wrong in the href structure? Not getting where I am doing wrong ?
Thanks
Siri K

This comment was edited by siri on Thursday 18th April 2024 at 01:54 GMT

Early Adopter

siri

User Level: Early Adopter

Date Joined: 19/07/2019

Posted by DaFa on Friday 19th April 2024 at 09:49 GMT

Hi Siri,

Sorry for the slow response - been hectic at work and haven't had much time to myself.

You're confusing me now.. the way you are creating the anchor tag with static methods is extravagant
<a href="<?php echo BASE_URL?>blog/<?php echo Blog::generateSlug($blog_categories[$i]['blog_title'])?>/<?php echo $blog_id; ?>"link</a>
Why not just use a url_string? This can be created with
$data['url_string'] = strtolower(url_title($data['blog_title']));
or use something link function _make_url_string_unique() from Trongate_pages.php. Then just add an anchor in your view
<?= anchor(BASE_URL . 'blog/' . $slug, 'link'); ?>
Then in your blog/blog_detail function I've given you 2 ways to get the blog details page from your db or my example lorem
function blog_detail() {
    // $sql = "SELECT * FROM `blog_details` WHERE `url_string` = '" . segment(3) . "'";//option 1 to get the details
    // $sel_res = $this->model->query($sql, 'array');

    // $sel_res = $this->model->get_one_where('url_string', segment(3), "blog_details"); //or option 2 in one line of code
    // $data = (array) $sel_res;

    $sel_res = "<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Itaque neque consequuntur ipsa dolorum. Iusto asperiores pariatur nemo laboriosam, ea hic!</p>";
    $data['blog_detail'] = $sel_res;

    $data['view_module'] = 'blog';
    $data['view_file'] = 'blog_detail';
    $this->template('public', $data);
}
and a simple 'arc_inner' view
<h1>Blog Detail - Title</h1>
  <?= $blog_detail ?>

This comment was edited by DaFa on Friday 19th April 2024 at 10:14 GMT

Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

Posted by siri on Friday 19th April 2024 at 12:43 GMT

Hi Simon,
Thank you for your reply. The main reason why I was trying to pass blog id , they were generating blog title not in a proper way. For example
Cloud ERP and Accounting :  Software Facts Your Business Should Know
AP Automation A Digital Transformation's Journey

something like above, So when i fetch related blog details I'm not able to fetch details from blog_details table.
Now I have impletemented what you suggested by using url_title(). This was generated urls like below
Original blog title - Cloud ERP and Accounting :  Software Facts Your Business Should Know 
generated blog title cloud-erp-and-accounting-software-facts-your-business-should-know
original one - AP Automation A Digital Transformation's Journey 
generated one - ap-automation-a-digital-transformation-s-journey

Now my question is how can i fetch details related to these blog title ?
$sql = "SELECT * FROM `blog_details` WHERE `url_string` ='ap-automation-a-digital-transformation-s-journey' ";

I was getting empty row because , there was no blog_title like this.
Thank you,
Siri K

This comment was edited by siri on Friday 19th April 2024 at 12:47 GMT

Early Adopter

siri

User Level: Early Adopter

Date Joined: 19/07/2019

Posted by DaFa on Saturday 20th April 2024 at 10:07 GMT

Hi Siri,

You need to ensure the url_string is unique and probably need to set some guidelines on how the user creates a blog title. I would suggest you take a look at how DC deals with the url_string in Trongate_pages.php
$this->validation_helper->set_rules('page_title', 'page title', 'required|min_length[2]|callback_title_check');
function title_check(string $str): string|bool {
        $page_title = trim(strip_tags($str));
        $page_title = preg_replace("/[ ]+/", " ", $page_title);
        $charset = (defined('CHARSET')) ? CHARSET : 'UTF-8';
        $page_title = htmlspecialchars($page_title, ENT_QUOTES, $charset);

        //make sure page title is unique
        $record_obj = $this->model->get_one_where('page_title', $page_title, 'trongate_pages');
        if ($record_obj !== false) {
            $error_msg = 'The page title that you submitted is not available.';
            return $error_msg;
        } else {
            return true;
        }
    }

This comment was edited by DaFa on Tuesday 23rd April 2024 at 22:40 GMT

Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

×