[英]How to use variable from routes.php in controller
In CakePHP have a bunch of unique URL names redirected in routes.php
file. 在CakePHP中,有一堆唯一的URL名称在
routes.php
文件中重定向。 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. 我想基于美化的名称创建
like
按钮的facebook。 In order to do that I need the $beautiful_urls
variable I use in the routes.php
in the Foo
controller. 为了做到这一点,我需要在
Foo
控制器的routes.php
中使用$beautiful_urls
变量。
How can I reach a variable in routes.php from a controller? 我如何从控制器访问routes.php中的变量? So far I tried to link it with
App::use('routes','Config');
到目前为止,我尝试将其与
App::use('routes','Config');
链接起来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 . 我不是cakephp用户,但简单的搜索显示有一个名为ClassRegistry的类。
You can create class BeautifulUrls and store it there. 您可以创建类BeautifulUrls并将其存储在此处。 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 另外,您可以使BeautifulUrls实现ArrayAccess接口,从而不必更改路由
I don't know if it's a good practice or not but my solution was to use the Configure class of CakePHP. 我不知道这是否是一个好习惯,但是我的解决方案是使用CakePHP的Configure类。 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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.