Php 如何在codeigniter中修改路由
帮我做这个 举个例子:我有一个普通的url “localhost/CI/index.php/base/storeurl” 如何让Codeigniter知道要查找 “localhost/CI/storeurl”Php 如何在codeigniter中修改路由,php,codeigniter,url,routing,Php,Codeigniter,Url,Routing,帮我做这个 举个例子:我有一个普通的url “localhost/CI/index.php/base/storeurl” 如何让Codeigniter知道要查找 “localhost/CI/storeurl” 我有一个名为index的函数,它在Base.php类中接受参数storeURL。帮我做这个。提前感谢。从WAMP环境中CodeIgniter的url中删除index.php只需要三个步骤 1) 与应用程序持有者并行创建.htaccess文件 只需复制以下代码: RewriteEngine
我有一个名为index的函数,它在Base.php类中接受参数storeURL。帮我做这个。提前感谢。从WAMP环境中CodeIgniter的url中删除
index.php
只需要三个步骤
1) 与应用程序持有者并行创建.htaccess
文件
只需复制以下代码:
RewriteEngine On
RewriteBase /CodeIgniter/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
2) 将应用程序文件夹中的config.php
中的$config['index\u page']
更改为空,如下所示:
$config['index_page'] = '';
3) 启用apache的重写模块
您也可以参考这里是一些伪代码和一些正确方向的要点,以帮助您入门 首先,您需要从URL中删除index.php。这里肯定有数百个问题已经回答了这部分问题,所以我将把这部分留给你们 正如许多人评论的那样,没有办法实现这种现成的url结构,它需要大量的代码才能工作。我实现这个特定设置的方法是将所有请求路由到单个控制器,并在该控制器内处理自定义路由 为此,在
routes.php
的底部添加一条路由,作为一条综合路线。其他控制器(新闻、事件等)的路由可以添加到此控制器之上。下面是一个示例routes.php,让您开始学习
config/routes.php
// Other routes go up here
// Catch all route
$route['(:any)'] = 'page/view';
// Home page route
$route['default_controller'] = "page/index";
class Page extends CI_Controller {
public function index()
{
// Do all code for your home page in here
}
public function view()
{
// Retrive the url string
$url = $this->uri->uri_string();
// Check if the corresponding view file exists
if (file_exists(APPPATH.'views/'.$url.'/index.php')){
// Load the view
$this->load->view($url.'/index');
} else {
show_404();
}
}
}
然后,您可以在controllers/page.php
中创建一个view
函数来检查url(使用codeigniters url helper),并加载与该url对应的正确视图
控制器/page.php
// Other routes go up here
// Catch all route
$route['(:any)'] = 'page/view';
// Home page route
$route['default_controller'] = "page/index";
class Page extends CI_Controller {
public function index()
{
// Do all code for your home page in here
}
public function view()
{
// Retrive the url string
$url = $this->uri->uri_string();
// Check if the corresponding view file exists
if (file_exists(APPPATH.'views/'.$url.'/index.php')){
// Load the view
$this->load->view($url.'/index');
} else {
show_404();
}
}
}
例如,一个用户转到http://yoursite.com/CI/about-us
,page Controller view函数将从url中获取字符串about us
,并将其设置为$url变量,然后搜索您的文件结构以检查相应的视图文件是否存在/application/views/about us/index.php
。如果有,它将加载该视图文件,如果没有,它将重定向到404
上面的代码都是从内存中键入的伪代码,因此可能无法直接工作,但希望能为您提供一条如何实现所需结果的线索。我终于找到了我想要的。下面是我的代码在routes.php中的样子
/* Custom Routes. */
// Store Normal Pages.
$route['home/(:any)'] = "base/home/$1";
$route['about/(:any)'] = "base/about/$1";
$route['services/(:any)'] = "base/services/$1";
$route['contact/(:any)'] = "base/contact/$1";
$route['feedback/(:any)'] = "base/feedback/$1";
// CategoryPage.
$route['category/(:any)/(:num)'] = "base/category/$1/$2";
// ProductPage.
$route['product/(:any)/(:num)'] = "base/product/$1/$2";
// For Main Home Page.
$route['(:any)'] = "base/home/$1";
我真的很感谢所有帮助我解决这个问题的人。谢谢你们 我总是不能上班。 最后我找到了解决办法。我只是想和大家分享,帮助有需要的人,因为我真的很喜欢codeigniter,因为它速度快,重量轻。 我正在处理动态类别标题,以显示在$this->uri->segment(1)上 路由将注意到,只要$this->uri->segment(2)是方法,它就会执行类的方法。您的url将非常漂亮:
base_url()/business-services/gallery/6
base_url()/travel/gallery/12
我还学到了路线必须是具体的。如果url类似于:
base_url()/travel/singapore/listing/1201
那么你的路线必须是:
$route['(:any)/(:any)/listing/(:num)'] = 'Class/listing';
如果您有要传入方法的param(取决于您是使用param还是uri_段)
玩得开心:)阅读文档:我只是不想删除index.php文件。但是类名,也就是base(在上面的示例中)。再看看我说的那些网址。它可以在routes.php中修改吗?不,你不能用codeigniter来修改。您可以使用mod_rewrite和htaccess在核心PHP中完成它。谢谢Jeemusu。我目前正在研究你在这里提到的同一个想法。如果效果好的话,我将与大家分享答案。你在这里运用的想法正是我现在所想的。我现在正在研究它。很高兴它对你有所帮助,当你刚开始的时候,你的头脑在codeigniter周围可能会很棘手。我已经得到了答案Jeemsu bro。我已经分享了代码。非常感谢。