如何从codeigniter中的URL中删除控制器名称? [英] How to remove controller name from the URL in codeigniter?

查看:68
本文介绍了如何从codeigniter中的URL中删除控制器名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器名称为profile,用户个人资料的网址为
www.example.com/profile/user



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

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

我要查找的是从URL中删除配置文件控制器名称,因此它将是SEO和用户



例如



www.example.com/user



有任何建议吗?

解决方案

您可以尝试以下任何一种



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

您的班级可能如下所示

  class Profile extends CI_Controller {

public function index($ id)
{
// $ id是您的param
}
}

更新: >

请记住,如果您有一个类 Someclass ,并且使用 url yourdomain / Someclass ,则将路由到 profile / index / $ 1 > $ route ['(:any)'] 或 $ route ['([a-zA-Z0-9] +)']


I have controller named "profile" and the URL for user profile is www.example.com/profile/user

I am already using the rerouting of codeigniter in the routes file

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

what I am looking is to remove the profile controller name from the URL so it will be SEO and user friendly.

e.g

www.example.com/user

any suggestions?

解决方案

You may try any one of these

// 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";

Your class may look like this

class Profile extends CI_Controller {

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

Update : (Be careful)

Remember that, if you have a class Someclass and you use url like yourdomain/Someclass then this will be routed to profile/index/$1 if you have $route['(:any)'] or $route['([a-zA-Z0-9]+)'].

这篇关于如何从codeigniter中的URL中删除控制器名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆