How to remove controller name from the URL in codeigniter?

前端 未结 4 534
南笙
南笙 2020-12-20 03:00

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

相关标签:
4条回答
  • 2020-12-20 03:36

    Try this:

    $route['user'] = 'profile/user';
    

    Dont know any generic way to do it

    Possible duplicate of How to hide controller name in the url in CodeIgniter?

    0 讨论(0)
  • 2020-12-20 03:46

    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]+)'].

    0 讨论(0)
  • 2020-12-20 03:50

    If I understand your question correctly, you just want to add a catchall to the very end of your routes file. Like:

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

    This will catch everything that isn't caught by another route, so make sure this script contains logic to determine when a 404 should be presented.

    0 讨论(0)
  • 2020-12-20 03:56

    You can use this in a route file:

    $route['(:any)'] = "controller_name/$1";
    $route['(:any)/(:any)'] = "controller_name/$1/$1";
    $route['(:any)/(:any)/(:any)'] = "controller_name/$1/$1/$1";
    
    0 讨论(0)
提交回复
热议问题