Route to redirect to a controller + an action by default on CodeIgniter?

偶尔善良 提交于 2019-12-04 15:05:42

There are a few ways to do this:

You could provide $action with 'display' by default:

function index($action = 'display'){}

OR

You could have a condition to physically redirect them

function index($action = ''){
   if(empty($action)){redirect('/cats/display');}
   //OTher Code
}

OR

You need to provide routes for when there is nothing there:

$route['cats'] = 'cat/index/display'; //OR the next one
$route['cats'] = 'cat/index'; //This requires an function similar to the second option above

Also, if you are only having a specific number of options in you route (ie. 'display', 'edit', 'new'), it might be worth setting up your route like this:

$route['cats/([display|edit|new]+)'] = 'cat/index/$1';

Edit:

The last route you created:

$route['cats'] = 'cat/display';

is actually looking for function display() in the controller rather than passing index the 'display' option

best way to use _remap function in your controller it will remap your url to specific method of controller

class Cat extends CI_Controller {

    function __construct(){
        parent::__construct();
    }

    function _remap($action)
    {
       switch ($action)
       {
            case 'display':
             $this->display();
            break;
            default:
               $this->index();
            break;
        }
    }

    function index($action){
        // code here
    }

    function display(){
        echo "i will display";
    }
    }

check remap in CI user guide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!