简体   繁体   English

如何在控制器中使用来自route.php的变量

[英]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.

相关问题 如何将第二个变量从route.php传递到Laravel 5中的控制器? - How to pass a second variable from routes.php to a controller in Laravel 5? 如何在Laravel route.php文件中使用供应商提供的控制器? - How to use controller from vendor in laravel routes.php file? 如何从Laravel中的route.php使用AuthServiceProvider? - How to use AuthServiceProvider from routes.php in Laravel? 如果CodeIgniter的网址中有某个字符串,如何告诉route.php不要使用控制器? - How to tell routes.php to NOT to use a controller if a certain string is in the url in CodeIgniter? 从Laravel 5.2中的route.php获取所有控制器和资源 - Get all the Controller & resources from the routes.php in Laravel 5.2 从 Laravel Routes.php 调用控制器函数 - Call Controller Function from Laravel Routes.php 如何在Laravel中从route.php调用静态函数 - How to call static function from routes.php in Laravel 如何使所有控制器路由默认为在route.php文件中建立索引 - How to make all controller routes to default to index in the routes.php file 如何缓存路由。Codeigniter 中的 php - How to cache routes.php in Codeigniter Codeigniter项目中的Controller如何在Routes.php中没有条目的情况下正确路由 - How can a Controller in a Codeigniter project be correctly routed without an entry in routes.php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM