CodeIgniter Forums
Add title for item as suffix in url, only for one controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Add title for item as suffix in url, only for one controller (/showthread.php?tid=70252)



Add title for item as suffix in url, only for one controller - kabeza - 03-13-2018

Hi
I've an application in CI 3.x
I have a special controller which I'd like to make more SEF

The actual uri is
http://www.myapp.com/index.php/item/detail/325

and I'd like to make it
http://www.myapp.com/index.php/item/detail/325/some-product-tag-slug-or-whatever

And also I'll remove index.php with .htaccess etc.

The question would be:
Is there any way to override site_url() and add the item's title (stored in db) as url suffix ?

I may create a model's function which can get title and then modify the 19 view files where I call item/detail controller/method but it will be a pain in the a**

Maybe another idea? Thanks


RE: Add title for item as suffix in url, only for one controller - salain - 03-14-2018

Hi,

PHP Code:
echo site_url('controller/method/'.$slug); 

You should also read the manual.
URL Helper

URI Route


RE: Add title for item as suffix in url, only for one controller - kabeza - 03-14-2018

(03-14-2018, 12:27 AM)salain Wrote: Hi,

PHP Code:
echo site_url('controller/method/'.$slug); 

You should also read the manual.
URL Helper

URI Route

Sorry, but You should also read the whole question
I stated that I'd like to avoid modifying/echoing site_url() again in the 19 view files and try to find some workaround (to solve this) in the controller itself, or routes file, or any other place

Thanks anyway


RE: Add title for item as suffix in url, only for one controller - salain - 03-14-2018

Show more code you may get a better answer


RE: Add title for item as suffix in url, only for one controller - natanfelles - 03-14-2018

You will need to adapt it in your model and set your route for item/detail/(:num)/(:any)...

PHP Code:
protected function setUri($data)
{
    if (isset(
$data['data']['title']))
    {
        
helper('text');

        
$data['data']['uri'] = url_title(strtolower(convert_accented_characters($data['data']['title'])));
    }

    return 
$data;


This enables you to call URL's like the StackOverflow (base_url/questions/(:num)/(:uri_title)).

This is a beforeInsert/beforeUpdate trigger for CodeIgniter 4, one row at a time.

Then you can get the item by primary key and have a SEF uri.