简体   繁体   中英

How to use variable from routes.php in controller

In CakePHP have a bunch of unique URL names redirected in routes.php file. Similar to this:

$beautiful_urls[0] = '/view/location-name/image-name.html';
Router::connect($beautiful_urls[0], 
                array('controller' => 'Foo','action' => 'bar',3,60));

I want to create facebook like buttons based on the beautified names. In order to do that I need the $beautiful_urls variable I use in the routes.php in the Foo controller.

How can I reach a variable in routes.php from a controller? So far I tried to link it with App::use('routes','Config'); but it's not working. I also thought about sending the values as action parameters, but that doesn't seem like good practice... I know it's not a great idea to mix the config file with a controller's logic but I don't have any better idea so far.

I'm not cakephp user but simple search shows that there is class called ClassRegistry .

You can create class BeautifulUrls and store it there. According to docs it's singleton and It can be accessed from everywhere.

Also you can make BeautifulUrls implement ArrayAccess interface so you don't have to change your routes

I don't know if it's a good practice or not but my solution was to use the Configure class of CakePHP. It was straightforward to use and accessible everywhere in the code and the config files.

You can save key-value pairs with

Configure::write('key','value');

and read it again with

Configure::read('key');

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