php - 如何从 codeigniter 的 URL 中删除 Controller 名称?

标签 php codeigniter

我有一个名为“profile”的 Controller ,用户配置文件的 URL 是 www.example.com/profile/user

我已经在路由文件中使用 codeigniter 的重新路由

$route['profile/(:any)'] = "profile/index"; 

我正在寻找的是从 URL 中删除配置文件 Controller 名称,以便它对 SEO 和用户友好。

例如

www.example.com/user

有什么建议吗?

最佳答案

你可以尝试其中任何一种

// url could be yourdomain/imran
$route['(:any)'] = 'profile/index/$1';
// url could be yourdomain/10
$route['(:num)'] = 'profile/index/$1';
// url could be yourdomain/imran10
$route['([a-zA-Z0-9]+)'] = "profile/index/$1";

你的类可能看起来像这样

class Profile extends CI_Controller {

    public function index($id)
    {
        // $id is your param
    }
}

更新:(小心)

请记住,如果你有一个类 Someclass 并且你使用 url 就像 yourdomain/Someclass 那么这将被路由到 profile/index/$1 如果你有 $route['(:any)']$route['([a-zA-Z0-9]+)' ]

关于php - 如何从 codeigniter 的 URL 中删除 Controller 名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19786855/

相关文章:

php - 如何在 Codeigniter 中内爆多个选择选项输入数组

php - 为数据库条目生成计数器代码

php - 用于searchly.com的Elasticsearch PHP连接设置

php - Python 脚本中关于 str(sys.argv[1]) 的错误

php - 无法更新 MYSQL

php - 在浏览器中从mysql数据库查看pdf文件

php - Codeigniter:对字段中的记录内容求和

javascript - Mapbox 行车路线

php - codeigniter where() postgresql

php - 单击提交时 Codeigniter 无法更新表单