【问题标题】:URL routing in PHP CodeigniterPHP Codeigniter 中的 URL 路由
【发布时间】:2016-11-12 12:47:27
【问题描述】:

我的 route.php 中有条目,例如 - $route['admin/students'] = 'view_student'。这里 view_student 是控制器名称。现在,当我从“localhost/school/admin”页面调用<a href="admin/students">Students</a> 时,一切正常;但是当我改变我的路线时 - $route['/school/admin/students'] = 'view_student',并从“localhost/school/admin”页面调用它为<a href="/school/admin/students">Students</a>,就会显示 404 页面。这里有什么问题?

【问题讨论】:

    标签: php codeigniter url-routing codeigniter-3


    【解决方案1】:

    school 是你的 ci 根,所以如果你定义 $route['/school/admin/students'],它将寻找具有管理功能的学校课程,而不是管理路线。

    在进行任何步骤之前,您应该先阅读文档, https://www.codeigniter.com/userguide3/general/routing.html

    【讨论】:

    • 好的.. 我明白了,但是当我在“localhost/school/admin/students/by_class”(学生是控制器类,by_class 是它的方法)页面上时问题仍然存在,而不是想要路由到“localhost/school/admin/register_student”(register_student 是控制器类),路由为$route['admin/register_student'],而不是 url 结构“localhost/school/admin/students/admin/register_student”而不是“localhost/school /admin/register_student”。如何做这种类型的路线?我的意思是如何跳过 url 中的 2 段 - 这里是“by_class”和“students”。
    • 如果我要像$route['register_student'] 这样的路由,那么 url 结构将是“localhost/school/admin/students/register_student”而不是“localhost/school/admin/register_student”。所以我的问题是如何从 URL 中跳过这个“学生”部分?
    【解决方案2】:

    试试这个可能对你有帮助的代码:

    这里的dashboard是控制器的名字

    //this will route as localhost/appFolder/admin/index
      $route['admin'] = 'dashboard'; // for your index page
    
    //this will route as localhost/appFolder/admin/method_name
     $route['admin/(:any)'] = 'dashboard/$1';
    
    //this will route as localhost/appFolder/admin/method_name/param1
    $route['admin/(:any)/(:any)'] = 'dashboard/$1/$2';
    

    链接路线Like

    // for your index page
    <a href="<?php echo base_url('admin/index'); ?>"></a>
    
    // for your other pages
    <a href="<?php echo base_url('admin/method_name'); ?>"></a>
    

    链接其他控制器的定义就像

     <a href="<?php echo base_url('otherControllerName/method_name'); ?>"></a>
    

    【讨论】:

    • 谢谢伙计!你刚刚救了我的命。从一两个星期开始就陷入这个问题。现在它工作得很好!但是我对你刚才提出的解决方案做了一些修改。向你致敬!再次感谢!
    • 像循环语句一样也节省了几行代码
    猜你喜欢
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多