[英]How to remove all controller names from url in codeigniter
I have multiple controllers in a Codeigniter project. 我在一个Codeigniter项目中有多个控制器。 I need to hide these controller names. 我需要隐藏这些控制器名称。 For eg: I have two controller, home and school. 例如:我有两个控制者,家庭和学校。 Each school have their own page which includes about,gallery,contact etc. and url should be http://www.sitename.com/schoolname . 每所学校都有自己的页面,其中包含关于,图库,联系方式等信息,网址应为http://www.sitename.com/schoolname 。 I hide home controller using routes.php. 我使用routes.php隐藏了家庭控制器。 $route['(:any)'] = 'home/$1'; $ route ['(:any)'] ='home / $ 1'; But it shows error in school controller. 但它显示了学校控制器中的错误。 Please help me..... Thanks. 请帮助我.....谢谢。
try this 尝试这个
$route['anything you want/(:any)'] = 'home/$1';
controller name is required so set it with any name you want 控制器名称为必填项,因此请使用所需的任何名称进行设置
It is not true that a controller name is absolutely required. 并非绝对需要控制器名称。
In routes.php
first define default controller to be home
(when user accesses homepage sitename.com) 在routes.php
首先将默认控制器定义为home
(当用户访问主页sitename.com时)
$route['default_controller'] = 'home';
Then you create the rule that any other page should be redirected to school
controller: 然后,创建规则,将任何其他页面都重定向到school
控制器:
$route['.*'] = 'school';
Now the home.php
controller will look like this: 现在, home.php
控制器将如下所示:
class Home extends CI_Controller {
public function Index()
{
echo "This is the homepage";
}
}
And in the school.php
controller you have to manually get the name of the school from requested URL: 在school.php
控制器中,您必须从请求的URL手动获取学校的名称:
class School extends CI_Controller {
public function _remap()
{
echo "User requested school: " . $this->uri->segment(1);
}
}
Why use _remap
method? 为什么要使用_remap
方法? Because it will be called everytime regardless of what's in URL or routing. 因为无论URL或路由中的内容是什么,它都会被每次调用。
If your controller contains a method named _remap(), it will always get called regardless of what your URI contains. 如果您的控制器包含一个名为_remap()的方法,则无论您的URI包含什么内容,它将始终被调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.