路由重定向到控制器+默认情况下CodeIgniter上的操作? [英] Route to redirect to a controller + an action by default on CodeIgniter?

查看:131
本文介绍了路由重定向到控制器+默认情况下CodeIgniter上的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Codeigniter开发一个项目。

I'm currently working on a project with Codeigniter.

我有一个名为Cat的控制器

I have one controller called Cat

class Cat extends CI_Controller {

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

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

}

和路线(在routes.php中)

and a route (in routes.php)

$route['cats/:any'] = 'cat/index/$1';

如果我使用这个URL例如: http://www.mywebsite.com/cats/display

And that works if I use this URL for example: http://www.mywebsite.com/cats/display

但是,如果用户将网址更改为 http://www.mywebsite.com/cats/ 工作了。 Codeigniter写道:404页面未找到 - 找不到您请求的页面。

Nevertheless, if the user changes the URL to http://www.mywebsite.com/cats/ it doesn't work anymore. Codeigniter writes: 404 Page Not Found - The page you requested was not found.

所以我的目标是将他重定向到 http://www.mywebsite.com/cats/display 默认情况下,如果他在cats / page

So my goal is to redirect him to http://www.mywebsite.com/cats/display by default if he is on the cats/ page

我需要做另一条路线吗?
我尝试了

Do I need to do another route? I tried

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

...但没有成功。感谢您的帮助。

...but without success. Thank you for your help.

推荐答案

有几种方法:

默认情况下,你可以使用'display'来提供$ action:

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

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

您可以有一个条件来物理重定向它们

You could have a condition to physically redirect them

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

您需要提供没有任何东西的路线: / p>

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

另外,如果你只有一个特定的您路线中的选项数量(例如'display','edit','new'),可能值得按如下设置路线:

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';

编辑:

您创建的最后一条路线:

The last route you created:

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

实际上是在寻找 function display()在控制器中,而不是通过index'display'选项

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

这篇关于路由重定向到控制器+默认情况下CodeIgniter上的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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