CodeIgniter Forums
Routing with variables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Routing with variables (/showthread.php?tid=80579)

Pages: 1 2


Routing with variables - chakycool - 11-18-2021

Hi,

Below is my routing but I can't get the last uri segment in to the function.

PHP Code:
$routes->get('/admin/reports/stores/:num''Admin/Reports/Brands::stores/$1',['filter'=>'accessfilter']); 

here is the function

PHP Code:
public function stores($id false){
    echo $id;


not sure what I'm doing wrong here  Sad


RE: Routing with variables - captain-sensible - 11-18-2021

try with '/admin/reports/stores/(:num)'




whats your class structure ?

For a Class Admin which has a method stores it would just be :
Code:
'Admin::stores/$1'



RE: Routing with variables - seunex - 11-18-2021

I'd should not be false as default since you are expecting an id in this controller.


RE: Routing with variables - captain-sensible - 11-18-2021

true well spotted


RE: Routing with variables - chakycool - 11-18-2021

Thanks guys for the response.

this '/admin/reports/stores/(:num)' did not work Sad

this the path to the controller "Admin/Reports/", the controller is "Brands" and the function is "stores" hence
PHP Code:
"Admin/Reports/Brands::stores/$1" 
.

this works "/admin/reports/stores/:num' but the value ":num" can not be picked up by the function "stores"

I'm on CI-4.14


RE: Routing with variables - captain-sensible - 11-18-2021

i would like you to try domain.something/admin/reports/stores/6 for web browser address bar at top url but make the route
Code:
$routes->get('admin/reports/stores/:num', 'Brands::stores/$1');
forget about the filter for now

and take out default value in method for now

i've taken out the forward slash in front of admin as well


RE: Routing with variables - chakycool - 11-18-2021

Yes the default routing works Big Grin

Any idea how to manage custom routing and have the auto routing off?

PHP Code:
$routes->setAutoRoute(false); 



RE: Routing with variables - captain-sensible - 11-18-2021

$routes->setAutoRoute(false); looks like you just did it !


section on mine is:

Code:
/**
* --------------------------------------------------------------------
* Router Setup
* --------------------------------------------------------------------
*/
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(false);


this is a sample of my routes in Routes.php

Code:
$routes->get('setUp','Setup::setUpForm');
$routes->post('setUpDo','Setup::process');
$routes->get('/', 'Home::index');
$routes->get('addProduct','Product::productAddForm');
$routes->post('addProduct','Product::addProductDo');

play around and yo uwill be fine ; make sure you have version control , i.e git i like "tags" to go back to incase their is a problem


RE: Routing with variables - chakycool - 11-18-2021

with the auto route off we can't pass these variable on the URI unless the URI class is used. So, any idea how can we have a custom routs with data been passed as my code below (which is not working)

PHP Code:
$routes->get('/admin/reports/stores/:num''Admin/Reports/Brands::stores/$1'); 



RE: Routing with variables - InsiteFX - 11-18-2021

Try this your missing the () on :num.

PHP Code:
$routes->get('/admin/reports/stores/:num''Admin/Reports/Brands::stores/$1'); 

// should be
$routes->get('/admin/reports/stores/(:num)''Admin\Reports\Brands::stores/$1'); 

Let me know if that works for you.

EDITED: according to @iRedds