简体   繁体   中英

CodeIgniter routing: Passing parameter before controller and catching it on the default_controller

I'm using a site wide parameter that controls the layout of the website. This parameter goes before anything else in the URL and is followed by the usual controller/parameters sections.

mywebsite/{section}/controller/parameter1/parameter2

I could define routes like the one bellow for each controller, but would like to avoid it as the section parameter is only relevant to the layout and not to the controllers.

$route['(:any)/controller/(:any)/(:any)'] = 'controller/$2/$3/$1';

Instead I would to be able to make the default_controller catch the first segment and store it as a view variable or session / cookie, so that the rest of the controllers can simply ignore it.

$route['(:any)/controller/(:any)/(:any)'] = 'controller/$2/$3';

As Hashem Qolami said, this is something much better served with a session or cookie .

Routing in CodeIgniter does not alter URL's. It allows you to alter what CI does with the URL. You can use server-side functionality to do what you want (eg apache rewrites, nginx conf's, etc) but that is out of scope here.


Edit:

To do what you want without changing the URL, you can just pass the value to the view:

Controller:

$layout = $this->uri->segment(1);
$data["layout"] = $layout;
$this->load->view("cool_view", $data);

cool_view.php:

    <link rel="stylesheet" type="text/css" href="<?php echo $layout; ?>.css">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM