Welcome Guest, Not a member yet? Register   Sign In
Routing - ignore first segment

Hi all!
I'm cracking my head over this but can't figure it out. I'm trying have the controller being specified in the second segment of the url.
So for instance http://site.eu/code/object/method... would end up invoking Object::method.

Obviously, I can manually list all my controllers and methods but I was hoping I could achieve this with just one routing instruction.
Thanks in advance for any ideas.

can you show your code so that i can understand exactly what you want to achieve.


Per your example, This works for me - it's all about your route in Config/Routes.php

$routes->get('/foobar/home/index', 'Home::index');

Above calls the right Home controller class/method but displays the uri with 'foobar'




Thanks for the replies and sorry for not being more clear on what I want to achieve.

(07-15-2020, 05:20 AM)laynebay Wrote:
$routes->get('/foobar/home/index', 'Home::index');

I know I could do this but then it would work for Home::index only. What I look for is a one routing definition that would cover all of my controllers and methods. This is what I in fact tried and found not to deliver:
PHP Code:
$routes->add'(:segment)/(:segment)/(:segment)/(:any)''$2::$3/$4' ); 

Have you tried specifying all of them? There are no optional setting, you need to manually specify all routes segments.

PHP Code:
$routes->add'(:segment)/(:segment)/(:segment)/(:any)''$2::$3/$4' ); 
$routes->add'(:segment)/(:segment)/(:segment)''$2::$3' );
$routes->add'(:segment)/(:segment)''$2::index' );
$routes->add'(:segment)''Default::index' ); 

(This post was last modified: 07-16-2020, 07:27 AM by Jan Zelenka.)

That was a great tip and we're definitely heading somewhere. I did exactly as suggested but it seems the Controller is not parsed properly. Here is my URL and the error message:

URL: https://site.local/junk/tester/method
Error message: Controller or its method is not found: \App\Controllers$2::method

Does one of these work?

PHP Code:
$routes->add'(:segment)/(:segment)/(:segment)/(:any)''\$2::$3/$4' ); 
$routes->add'(:segment)/(:segment)/(:segment)''\$2::$3' );
$routes->add'(:segment)/(:segment)''\$2::index' );
$routes->add'(:segment)''\Default::index' ); 

$routes->add'(:segment)/(:segment)/(:segment)/(:any)''\\$2::$3/$4' ); 
$routes->add'(:segment)/(:segment)/(:segment)''\\$2::$3' );
$routes->add'(:segment)/(:segment)''\\$2::index' );
$routes->add'(:segment)''\\Default::index' ); 

Unfortunately, both give: Controller or its method is not found: $2::method


I'm no expert but seems to me perhaps you cannot fudge or fake the actual Class you wish to call. I see nowhere in the routing documentation that you can substitute like this. As long as I specify an actual controller, I can play around with everything else:

$routes->add( '(:segment)/(:segment)/(:segment)/(:any)', 'Shipment::$2/$3/$4');


Maybe Custom Placeholders?  Seems like having a single route to rule them all may not be feasible, but I'll leave it to someone else to confirm that!

Maybe start out with some ore basic routing, make sure everything is working as expected, then try to tweak as desired.

"Premature optimization" is something I'm always reminding myself about.

Good luck.

laynebay, you are right, I have studied the documentation before turning here and it never mentions the ability to substitute the Controller with a placeholder. But purely technically speaking, it is a string and when CodeIgniter is capable of extracting the Controller and Method from a string it sure is capable of substituting the placeholders in any part of the definition.

If for any reason this is not supposed to be done, I will happily accept it and keep on the joyful experience of using CodeIgniter Wink but so far it looks more like it is supposed to work as mentioned by jreklund.

Theme © iAndrew 2016 - Forum software by © MyBB