【问题标题】:static Url rewriting in CodeigniterCodeigniter 中的静态 URL 重写
【发布时间】:2018-01-22 21:31:07
【问题描述】:

我的本​​地服务器中有一个 codeigniter 应用程序。 我需要以下一些 URL 重写规则。 如果哪位有什么解决办法请回答。

http://localhost/myapp/index.php/users/login

http://localhost/myapp/index.php/usuarios/login

如何在 Codeigniter 中执行此操作。

【问题讨论】:

    标签: php .htaccess codeigniter url-rewriting


    【解决方案1】:

    config/routes.php 中的这条记录会做你想做的事 -

    $route['users/login'] = 'usuarios/login'; // for login only (static)
    $route['users/(:any)'] = 'usuarios/$1'; // for all routes to users (dynamic for functions without parameters)
    

    更多细节在这里:URI Routing

    【讨论】:

    【解决方案2】:

    Codeigniter 以这样的方式从 Mycontroller 访问 myFunction,其值为 myVarValue

    http://my.webPage.com/index.php/MyController/myFunction/myVarValue
    

    你想改变 URL,是的,你可以通过在 ../application/config/routes.php 上创建路由

    $route['my-function-(:any)'] = 'MyController/myFunction/$1';
    

    这是第一步

    您创建了路线,但这些路线不起作用?

    1. 在文件 ../application/config/config.php 上设置 base_url,在您的情况下为 http://localhost/myapp,文件的第 26 行CI v3.1.6
    2. 设置默认控制器,任何进入你网站的人都会立即访问的默认控制器,你在../application/config/routes.php上设置这个,你的默认控制器可以是控制器的名称或控制器中的功能。

    假设你有这个控制器

    public class MyController extends CI_Controller{
        public function __construct(){
            parent::__construct();
        }
        public function index(){
            echo 'Hello people, im the index of this controller,'.
                 ' im the function u will see everytime u '.
                 'access the route http://localhost/myApp/MyController';
        }
    
        public function notTheIndex(){
             echo 'Hello, im the function u will see if u'.
                  'access http://localhost/MyController/notTheIndex';
        }
    }
    

    假设这个控制器将是您的默认控制器,如 ci v3.16 上第 53 行的 routes.php 文件中所述 set

    $route['default_controller'] = 'MyController';
    //if u want to just access the index method
    
    $route['default_controller'] = 'MyController/notTheIndex';
    //if for some reason u have a method u would like to be executed 
    //by default as the index page
    
    1. 现在您希望 uri 保留您设置的名称

    $route['some-beautiful-name'] = 'MyController/notTheIndex';

    所以当你访问 http://localhost/some-beautiful-name 时,它会显示 MyController/notTheIndex 的结果

    考虑如果您通过<a href='some_link'>Some link</a> 访问不同的页面并且需要将href 值更改为您定义的路由,如果没有,如果它们指向控制器,这仍然可以工作,但uri 名称将保留同样,我们以'some.beautiful-name'为例来访问MyController/notTheIndex

    在你看来

    <a href="<?=base_url()?>MyController/notTheIndex">This is a link</a>
    

    链接有效,但 uri 名称将是 http://localhost/MyController/notTheIndex

    <a href="<?=base_url()?>some-beautiful-name">This is a link</a>
    

    链接会起作用因为你定义了这样的路由否则它不会并显示 404 错误,但这里显示的 url 将是 http://localhost/some-beautiful-name

    希望我的回答对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多