【问题标题】:access controller inside the controller directory within another folder在另一个文件夹中的控制器目录中访问控制器
【发布时间】:2017-09-10 04:41:23
【问题描述】:

这是我的控制器目录结构

controllers
-----user(folder)
----------User_reg.php //My Controller inside user folder.

-----index
-----Welcome.php

我可以像这样访问User_reg.php

http://localhost/coin_system/user/user_reg

现在我想从这个 URL 中删除 user 所以我添加了路由

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

但它显示错误:404 Page Not Found

我想这样访问它

http://localhost/coin_system/user_reg

如何访问控制器目录中的控制器?

我尝试使用this SO question 解决,但没有帮助。 我正在使用 Codeigniter 最新版本。 3.1.5

【问题讨论】:

    标签: php codeigniter url


    【解决方案1】:

    你错过了功能

    https://www.codeigniter.com/user_guide/general/routing.html#examples

    $route['user_reg'] = 'user/user_reg/index';
    $route['user_reg/(:any)'] = 'user/user_reg/index/$1';
    

    或者你可以有不同的功能

    $route['user_reg'] = 'user/user_reg/somefunction'
    $route['user_reg/(:any)'] = 'user/user_reg/somefunction/$1';
    

    也可以尝试在 url 中使用 index.php

    http://localhost/coin_system/index.php/user_reg
    

    【讨论】:

    • 所以我每次在这个控制器中创建函数时都必须编写一个新路由
    • 先生,您能否解决我每次在此控制器中创建新功能时都必须创建新路由的疑问
    【解决方案2】:

    当您使用:any 添加路由时,它还会在控制器之后找到您的调用方法。但是对于索引(默认),它不是所有索引方法都必须的,所以你还需要为它指定路由。所以只需要再增加一条路线

    $route['user_reg'] = 'user/user_reg'; // add this to route file
    $route['user_reg/(:any)'] = 'user/user_reg/$1';
    

    【讨论】:

    • 谢谢您的解决方案正在运行,先生,但您能否澄清一个疑问,如果我使用这种方式,那么我必须在创建新功能时创建新路线?
    • No (:any) => $1 将与您创建的任何功能相关
    • 最后一个问题先生,这种方式对用户是否可靠,或者我应该继续使用旧的(常规)方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多