自定义路由和控制器功能

时间:2013-09-24 18:26:53

标签: php codeigniter controller routing

我的 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');
        }
    }

上一页我刚刚开始使用CodeIgniter,我从基础教程中得到的内容以及在阅读了许多stackoverflow答案后,domain/login的来电将被路由到function loginPages class (控制方)中,根据路由规则$route['login'] = 'pages/login';

问题:这个简单的代码显示404错误。我不知道为什么会这样,因为所有文件都存在于模板文件夹中。此外,对域的正常调用工作正常,但如果我调用domain/home,则再次出现404错误。请帮助我做错的事。

2 个答案:

答案 0 :(得分:1)

所以我对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. 重新启动服务器

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

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

    1. 检查URI中的domain / front_controller / route模式,查看路由是否存在并正确转发
    2. 通过域/登录,您没有遵循该模式,因此交付了404。将front_controller(index.php)添加到URI使其遵循路由模式并转发到您的路由配置。

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

答案 1 :(得分:1)

  

现在搞定了!

  1. 实际上定义了

    base_url = 'mysite.com'

    config.php中的

    只适用于在路由规则中调用default_controller,因此您的 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添加到路由器然后将正常工作!!

  3. 对于读出来的人来说,一个很好的建议就是在解决疑问之前彻底阅读文档。 :)