简体   繁体   English

我可以在codeigniter路由内使用帮助程序吗?

[英]Can I use a helper inside codeigniter route?

So I have a codeigniter helper that determines wether the signed request is from mobile or not. 因此,我有一个Codeigniter帮助器,它确定签名的请求是否来自移动设备。 and so I would like that my url routes to different controllers when there is mobile request. 因此,当移动请求出现时,我希望将我的网址路由到其他控制器。

for example 例如

if (get_request() === 'mobile')
$route['u/a/(:any)'] = "mobile/usr/main_controller/game_detail/$1";
else
$route['u/a/(:any)'] = "pc/usr/main_controller/game_detail/$1";

thanks in advance! 提前致谢!

In short you can't use autloaded helpers and you can't use $this inside a helper. 简而言之,您不能使用自动加载的帮助程序,也不能在帮助程序内部使用$ this。 Unlike libraries helpers aren't classes so you can't get CodeIgniter's instance. 与库不同,助手不是类,因此您无法获取CodeIgniter的实例。

However to solve your problem you can however include the helper file. 但是,要解决您的问题,您可以包括帮助文件。

require_once( APPPATH .'helpers/your_helper.php');

and so because its already included you can now use this helper's function like get_request() etc.. 因此,由于它已经包含在内,因此您现在可以使用此助手功能,如get_request()等。

if (get_request() === 'mobile')
$route['u/a/(:any)'] = "mobile/usr/main_controller/game_detail/$1";
else
$route['u/a/(:any)'] = "pc/usr/main_controller/game_detail/$1";

and then you can use this now. 然后您就可以使用它了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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