• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Routing - ignore first segment

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

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

#3
Jan,

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

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

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

http://domain.com/foobar/home/index

HTH

Michael
Reply

#4
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:
Code:
$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' ); 
Reply

#5
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' ); 
Reply

#6
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
Reply

#7
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' ); 
Reply

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

#9
Jan,

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:

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

Routing

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

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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