【问题标题】:Custom routing and controller function自定义路由和控制器功能
【发布时间】:2013-09-30 02:54:24
【问题描述】:

我的 routes.php

$route['home'] = 'pages/home';
$route['login'] = 'pages/login';
$route['default_controller'] = 'pages/home';

和控制器 pages.php 一样

class Pages extends CI_Controller {

    public function home() {
        $this->load->view('templates/header');
        $this->load->view('pages/home');
        $this->load->view('templates/one');
        $this->load->view('templates/two');
        $this->load->view('templates/footer');
        }

    public function login() {
         //if ( ! file_exists('application/views/templates/'.$page.'.php')) {
     //     echo "no file";
     //   }
     //  $data['title'] = ucfirst($page);
        $this->load->view('templates/header');
        $this->load->view('templates/login');
        $this->load->view('templates/footer');
        }
    }

Pre: 我刚开始使用 CodeIgniter,我从基本教程中得到的,在阅读了许多 stackoverflow 答案后,对 domain/login 的调用将被路由到 Pages class 中的 function login (controler) 根据路由规则$route['login'] = 'pages/login';

问题:这个简单的代码显示 404 错误。我不明白为什么会这样,因为所有文件都存在于模板文件夹中。对域的正常调用也可以正常工作,但如果我调用domain/home,我会再次收到 404 错误。请帮助我做错了什么。

【问题讨论】:

  • 起初我确信你一定是某个地方有错字。然后我进入我的代码并以相同的方式创建了一条路线。 BINGO 我得到相同的 404。我将尝试调试它。可能是最新版本的路由器代码中的错误。在那之前,我已经 +1 了这个问题。
  • 我发现了这个问题。答案如下。

标签: php codeigniter controller routing


【解决方案1】:

现在明白了!

  1. 实际定义

    base_url = 'mysite.com'

    in config.php 仅适用于在路由规则中调用 default_controller,因此您的 while mysite.com 调用将正常工作并显示主页, *解释 default_controller* 路由规则,但是任何对 mysite.com/xyz 的调用都会失败,即使您在主控制器中有一个函数 xyz 并且路由规则为 $route['xyz'] = 'pages/home',

    其余的所有 URL 调用都必须作为

    domain/index.php/controller/function/arg,

  2. 正如@Bill Garrison 和开发人员user guide of codeigniter 所建议的那样,应该在.htaccess 中编写规则以从域名中删除index.php,然后路由器的url 将作为正常!!

对于阅读的人来说,一个重要的建议是在提出疑问之前彻底阅读文档。 :)

【讨论】:

    【解决方案2】:

    所以我对 CodeIgniter 也有点陌生,所以我很抱歉在这方面太慢了。您面临的问题是您没有放入 index.php。您的 URL 必须是 domain/index.php/login。如果您不想在每次调用中添加 index.php,那么您必须执行以下操作:

    1. 在您的应用程序文件夹中添加一个 .htaccess 文件,使其看起来像这样:

      <IfModule mod_rewrite.c>
      
          # activate URL rewriting
          RewriteEngine on
      
          # the folders mentioned here will be accessible and not rewritten
          RewriteCond $1 !^(resources|system|application|ext)
      
          # do not rewrite for php files in the document root, robots.txt or the maintenance page
          RewriteCond $1 !^([^\..]+\.php|robots\.txt)
      
          # but rewrite everything else
          RewriteRule ^(.*)$ index.php/$1 [L]
      </IfModule>    
      <IfModule !mod_rewrite.c>
      
          # If we don't have mod_rewrite installed, all 404's
          # can be sent to index.php, and everything works as normal.
      
          ErrorDocument 404 index.php
      
      </IfModule> 
      
    2. 开启mode_rewrite(How to enable mod_rewrite for Apache 2.2https://askubuntu.com/questions/48362/how-to-enable-mod-rewrite-in-apache

    3. 重启服务器

    这会将所有域/登录请求转发到您的前端控制器 (index.php)。 RewriteCond $1 !^(resources|system|application|ext) 行将允许您使某些文件夹不被重写。因此,如果您在应用程序下有一个名为“resources”的文件夹,而不是将其转发到 domain/index.php/resources,它只会转到 domain/resources。

    实际上没有 .htaccess 文件的过程是这样的:

    1. 检查 URI 中的 domain/front_controller/route 模式并查看路由是否存在并正确转发

    通过进行域/登录,您没有遵循该模式,因此发送了 404。将 front_controller (index.php) 添加到 URI 使其遵循路由模式并转发到您的路由配置。

    有些人认为他们的 URI 中的前端控制器“丑陋”,所以他们添加了一个 mod_rewrite,基本上每次访问该目录时都会将 front_controller 添加到 URI 中。这样他们就可以坚持域/控制器/操作。这也被认为更安全,因为唯一可以直接访问的目录是重写中特别说明的目录。

    【讨论】:

    • 感谢您的回复。好的..这行得通!但是如果我们在config.php 中已经有$config['base_url'] = 'domain'$config['index_page'] = 'index.php',那么问题仍然存在,那么为什么需要对 mod_rewrite 进行显式更改。
    • 它们是两种不同的做事方式:使用 mod_rewrite 你跳过 index.php 并且在你的配置中它实际上应该是空白的。缺点是基础应用程序目录中的任何文件夹都必须在 mod_rewrite 中声明。这是更安全的imo。当您的应用程序在 URI 中看到 $config['index_page'] 配置时,没有 mod_rewrite,它知道之后会将信息转发到路由器。在配置中更改它只是告诉应用程序寻找不同的前端控制器。 ...我会编辑我的回复以更好地解释
    • hmm.. 还有什么办法,通过 PHP 本身而不触及 .htaccess 文件,我的意思是一定有办法,毕竟我使用的是框架:\
    • 网址也可以是domain/controller/method!并且路由规则只是将domain/login转发到controller-&gt;method()
    • 是的......据我所知......应用程序将 URI 发送到路由器的唯一时间是当 URI 中声明的控制器/方法不存在时。跨度>
    猜你喜欢
    • 2017-06-09
    • 2015-10-12
    • 2018-09-14
    • 2016-07-04
    • 2011-11-24
    • 1970-01-01
    • 2012-07-20
    • 2015-02-07
    • 1970-01-01
    相关资源
    最近更新 更多