• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to setup a route for an optional second param?

#1
Do I need to setup two rules in the Routes.php for these two urls?

Code:
1) http://mydomain.example/show-list
2) http://mydomain.example/show-list/25
PHP Code:
Rule 1$routes->add('show-list''Home::index');
Rule 2$routes->add('show-list/(:any)''Home::index'); 
Is there any way to achieve that in on rule? Just trying to avoid having to repeat it.

PHP Code:
//HomeController.php 
public function index()
{
 
$user_id $this->request->uri->getSegment(2);
 if(
$user_id != NULL) {
 
//filter data by given user_id
 
} else {
 
//show all data
 
}
 return 
view('listdata'compact('data');


If I have only this rule
Code:
$routes->add('show-list', 'Home::index');
then it throws 404 file not found for url http://mydomain.example/show-list/25

If I have only this rule
Code:
$routes->add('show-list/(:any)', 'Home::index');
then it throws 404 file not found for url http://mydomain.example/show-list
Reply


Messages In This Thread
How to setup a route for an optional second param? - by happyape - 03-11-2019, 10:03 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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