1,设置路由
默认控制器设置文件
application/config/routes.php
$route['default_controller'] = 'welcome';
我们新建一个控制器文件
application/controllers/article.php
<?php class Article extends CI_Controller{ public function index(){ echo "这是一个Article的index方法"; } }
访问:index.php/article/index
在写一个方法
public function show($id){ echo '这是文章'.$id; }
访问:index.php/article/show/4
或者index.php/article/show/4.html
我们现在路由配置文件里面写一个正则
application/config/routes.php
//index.php/news/201654/4.html $route['news/[\d]{6}/([\d]+)\.html']='article/show/$1';
2,去掉index.php
前提是需要确定Apache开启rewrite模块
wamp/bin/apache/apache2.4.9/conf/httpd.conf
打开注释
LoadModule rewrite_module modules/mod_rewrite.so
让后修改项目配置文件.htaccess文件,添加代码
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] </IfModule>
这样就可以去掉index.php
如果访问错误,再去application/config/config.php文件里
$config['index_page'] = 'index.php';//把index.php去掉