将post id作为codeigniter中的第二个段传递

Kin*_*ien 0 php codeigniter

我想为artcles建立以下网址:http://mysite.com/articles/ ID.ID是文章的ID,当然不是常数.如何使用codeigniter完成?据我所知,第二段应该是控制器内的方法名称...

saf*_*rov 7

它简单,Codeigniter具有强大的URL路由

 http://mysite.com/article/id
Run Code Online (Sandbox Code Playgroud)

在你的路由器

$route['articles/([0-9]+)'] = "controller_name/article/$1";
Run Code Online (Sandbox Code Playgroud)

并在你的控制器

public function article($id) {
  //here your article id
  echo $id;
}
Run Code Online (Sandbox Code Playgroud)

此外,您需要在.htaccess上转发一些URL以将所有组合转发到index.php

RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Run Code Online (Sandbox Code Playgroud)