codeigniter如何为控制器类和方法设置路由? [英] codeigniter how do i setup routing for controller class and method?

查看:85
本文介绍了codeigniter如何为控制器类和方法设置路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CI新手,需要专家的一些初学帮助。

I am new to CI and need some beginner's help from experts.

这是我目前的设置:
/ controllers /

Here is what my current setup is: /controllers/


  • home.php

  • report.php

/ views /

/views/


  • home / index.php

  • /recent.php

  • report / index.php

  • report / generate.php

  • home/index.php
  • home/recent.php
  • report/index.php
  • report/generate.php

我试图产生的URI作为结果:

the URI i am trying to produce as an outcome:

http:// localhost
http:// localhost / report (将加载索引.php)
http:// localhost / report / generate (会调用生成的方法报告控制器)

http://localhost http://localhost/report (would load the index.php) http://localhost/report/generate (would call the method for generate in the report controller)

http:// localhost / recent / 10 < a>(将调用在home控制器中生成的方法传递变量'10')

http://localhost/recent/10 (would call the method for generate in the home controller passing the variable '10')

$route['default_controller'] = "home";
$route['404_override'] = '';
$route['/'] = 'home/index';
$route['recent/(:num)'] = 'home/recent/$1';
$route['report/(:any)'] = 'report/$1';

如何避免为类中创建的每个新方法总是修改路由文件?所以它将遵循:
$ route [$ controller / $ method / $ variable](非常用于如何设置.net mvc路由)。

How do i avoid always modifying the routes file for each new method created in a class? so that it would follow: $route[$controller/$method/$variable] (very use to how .net mvc routing is setup).

推荐答案

您不需要进一步修改。事实上,即使这一行是多余的:

You don't need further modifications. In fact, even this line is redundant:

$route['report/(:any)'] = 'report/$1';

这一个也是多余的:

$route['/'] = 'home/index';

,因为默认控制器设置为home,默认方法 index

since the default controller is set to 'home' and the default method is always index.

查看CI如何与URL结合使用: https://ellislab.com/codeigniter/user-guide/general/urls.html

Look at how CI works with URLs: https://ellislab.com/codeigniter/user-guide/general/urls.html

因此, / localhost / report / generate 会查找 Report 控制器, c $ c>生成方法。

So, /localhost/report/generate would look for the Report controller, and load its generate method. That's how it works out-of-the-box, no routing needed.

这条路线很好:

$route['recent/(:num)'] = 'home/recent/$1';

如果将URL / localhost / recent / 123 并将加载 Home ,控制器,最近 / code>作为第一个方法参数。

If will take the URL /localhost/recent/123 and will load the Home, controller, recent method and will pass 123 as the first method parameter.

这篇关于codeigniter如何为控制器类和方法设置路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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