【发布时间】:2016-05-03 11:36:11
【问题描述】:
我有控制器 Post 和方法 index、add 和 delete。我为此操作创建路由规则:
$route['posts'] = 'post/index'; // <-- Work
$route['post-add'] = 'post/add';// <-- Work
$route['post-delete/(:num)'] = 'post/delete/$1'; // <-- Not Work
第一条路线www.example.com/posts 运行良好,第二条路线/post-add 运行良好。但是当我打电话给post-delete/5 这不起作用时,我总是得到404 Not Fount。
这是控制器
class Post extends MY_Controller
{
public function index()
{
//.
}
public function add()
{
//.
}
public function delete($id)
{
echo "Delete post #ID =" $id;
}
}
我只对delete 路线有问题,我不知道他为什么不接受那个参数。
工作:
- www.example.com/posts
- www.example.com/add
不起作用:
- www.example.com/delete/1
可能有什么问题?
【问题讨论】:
-
可能是你忘记在你的控制器中添加
$this->load->helper('url'); -
试试
$route['post/delete/(:num)'] -
@Maninderpreet Singh
url在配置中自动加载。 -
@Saty 当我尝试你的例子时,我得到了一些
Not Found The requested URL /post/delete/1 was not found on this server. -
post/delete/$1应该可以在不重写路由的情况下工作。在routes.php中不做任何更改,使其首先工作。
标签: php codeigniter