• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add title for item as suffix in url, only for one controller

#1
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/deta...r-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
Reply

#2
Hi,

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

You should also read the manual.
URL Helper

URI Route
A good decision is based on knowledge and not on numbers. - Plato

Reply

#3
(03-14-2018, 01: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
Reply

#4
Show more code you may get a better answer
A good decision is based on knowledge and not on numbers. - Plato

Reply

#5
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.