【问题标题】:default routing in codeignitercodeigniter 中的默认路由
【发布时间】:2013-05-11 20:33:46
【问题描述】:

我正在探索 codeigniter。在应用程序启动时,默认控制器更改为加载我的控制器。

控制器正确加载视图,这很好,所以我猜路由按预期工作,但是当我使用(在同一控制器上的地址栏上手动键入其他方法)相同的 url 模式 /controller/method 我得到 404错误,两种视图都存在。

是否必须更改某些默认路由行为或其他问题?

谢谢

【问题讨论】:

  • 您可能需要一个 htaccess 文件。谷歌获取 CodeIgniter htaccess 文件。
  • @user1765862 :你必须使用 index.php/controller/method/id 除非你使用 .htaccess 来重写 url。很容易将 .htaccess 文件放在 docs 根目录中,.htaccess 的内容可以在 codeigniter 用户指南中找到

标签: php codeigniter


【解决方案1】:

我不知道您是否已经从您的 url 模式中删除了 index.php,假设您应该在浏览器地址字段中输入 index.php/controller/method。 (如果您按照描述手动输入 url)

另一方面,如果您不想在每个链接上使用 index.php,您可以考虑将其删除,more info here

【讨论】:

    【解决方案2】:

    这可能是因为上面提到的 index.php 文件,或者如果您想摆脱 index.php,请在您的应用程序中包含 .htaccess 文件。

     config/config.php - modifiy 
     $config['base_url'] = 'index.php'
     $config['base_url'] = '' // set it to blank
    

    .htaccess 文件参考以下代码

     RewriteEngine on
     RewriteCond $1 !^(index\.php|images|robots\.txt)
     RewriteRule ^(.*)$ /index.php/$1 [L]
    

    【讨论】:

      【解决方案3】:

      关注这个

      root_folder/.htaccess

      删除网址中的index.php

      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php/$1 [L]
      

      设置基本网址

      root_folder/application/config/config.php

      | to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
      | The auto-detection mechanism exists only for convenience during
      | development and MUST NOT be used in production!
      |
      | If you need to allow multiple domains, remember that this file is still
      | a PHP script and you can easily do that on your own.
      |
      */
      $config['base_url'] = 'http://[::1]/my-project/';
      

      删除 url 中的 index.php,即使是在表单中的请求发布时

      root_folder/application/config/config.php

      /*
      |--------------------------------------------------------------------------
      | Index File
      |--------------------------------------------------------------------------
      |
      | Typically this will be your index.php file, unless you've renamed it to
      | something else. If you are using mod_rewrite to remove the page set this
      | variable so that it is blank.
      |
      */
      $config['index_page'] = '';
      

      s设置默认控制器,我的是 'home'

      root_folder/application/config/routes.php

      | controller and method URI segments.
      |
      | Examples: my-controller/index -> my_controller/index
      |       my-controller/my-method -> my_controller/my_method
      */
      $route['default_controller'] = 'home';
      

      之后,确保所有控制器文件名都是大写的。 一个也是一个类名。

      当您需要实时上传时,这也很重要 服务器。

      root_folder/application/controllers/Home.php

      <?php
      
      /**
       * 
       * 
       * @author Lloric Garcia <emorickfighter@gmail.com>
       */
      defined('BASEPATH') OR exit('No direct script access allowed');
      
      class Home extends MY_Controller {
      
          public function index() {
      
          }    
      
      }
      

      那么这就是你的网址

      http://[::1]/my-project/home


      这是我在实时服务器中的设置

      这一切都来自

      https://www.codeigniter.com/userguide3/index.html

      【讨论】:

        猜你喜欢
        • 2016-10-30
        • 2016-01-18
        • 1970-01-01
        • 2013-10-22
        • 1970-01-01
        • 2017-01-19
        • 1970-01-01
        • 2016-03-27
        • 2014-05-05
        相关资源
        最近更新 更多