如何从codeigniter中的URL中删除控制器名称?

Imr*_*med 2 php codeigniter

我有名为"profile"的控制器,用户档案的URL是www.example.com/profile/user

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

$route['profile/(:any)'] = "profile/index"; 
Run Code Online (Sandbox Code Playgroud)

我正在寻找的是从URL中删除配置文件控制器名称,以便它将是SEO和用户友好.

例如

www.example.com/user

有什么建议?

The*_*pha 6

您可以尝试其中任何一个

// 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";
Run Code Online (Sandbox Code Playgroud)

你的班级可能看起来像这样

class Profile extends CI_Controller {

    public function index($id)
    {
        // $id is your param
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:(小心)

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