简体   繁体   English

如何将值/变量直接传递给Codeigniter中的默认控制器?

[英]How to pass value/variable directly to default controller in codeigniter?

Assume base_url is " http://www.facebook.com " 假设base_url是“ http://www.facebook.com

Default controller is "Home" 默认控制器为“主页”

How do i code my routes and controller to get my URL as " http://www.facebook.com/noddycha " 我该如何编码我的路线和控制器以获取URL为“ http://www.facebook.com/noddycha

Where "noddycha" is a get parameter. 其中“ noddycha”是一个get参数。 The page should load my profile page from DB. 该页面应从数据库加载我的个人资料页面。

In your application/routes.php file add this line after $route['404_override'] = ''; 在您的application / routes.php文件中,在$route['404_override'] = '';之后添加此行$route['404_override'] = ''; :

$route['(:any)'] = 'users/profile';

Now absolutely everything will be redirected the profile function of the users controller (or whichever controller/function combination you substituted in the routes.php file). 现在,绝对所有内容都将重定向到users控制器的profile功能(或您在routes.php文件中替换的任何控制器/功能组合)。 Then you can get the parameter by doing this in the profile function: 然后,您可以通过在profile函数中执行以下操作来获取参数:

if ($this->uri->total_segments() === 1) {
    $profile = $this->uri->total_segment(1);
}

If you have some other controllers which you would still like to be able to access, eg an about us page, add them after the above line in the routes.php file. 如果您还有一些其他控制器仍想访问,例如“关于我们”页面,则将它们添加到routes.php文件中的上述行之后。 For example, if we extend the example above: 例如,如果我们扩展上面的示例:

$route['(:any)'] = 'users/profile';
$route['home/about'] = 'home/about_us';

Would mean that every request is sent to the users/profile function, except http://example.com/home/about will go to the about us page. 这意味着每个请求都将发送到users/profile功能,除了http://example.com/home/about之外,将转到“关于我们”页面。

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

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