Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自定义路由和控制器功能_Php_Codeigniter_Controller_Routing - Fatal编程技术网

Php 自定义路由和控制器功能

Php 自定义路由和控制器功能,php,codeigniter,controller,routing,Php,Codeigniter,Controller,Routing,我将我的routes.php作为 $route['home'] = 'pages/home'; $route['login'] = 'pages/login'; $route['default_controller'] = 'pages/home'; class Pages extends CI_Controller { public function home() { $this->load->view('templates/header');

我将我的routes.php作为

$route['home'] = 'pages/home';
$route['login'] = 'pages/login';
$route['default_controller'] = 'pages/home';
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');
        }
    }
控制器pages.php

$route['home'] = 'pages/home';
$route['login'] = 'pages/login';
$route['default_controller'] = 'pages/home';
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后得到的答案是,
域/登录
的调用将被路由到
页面类中的
函数登录
(控制程序),根据路由规则
$route['login']='页面/登录'


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

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

  • 在应用程序文件夹中添加一个.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> 
    
    
    #激活URL重写
    重新启动发动机
    #此处提及的文件夹将可访问且不会重写
    1美元^(资源|系统|应用|扩展)
    #不要在文档根、robots.txt或维护页面中重写php文件
    1美元^([^\..]+\.php | robots\.txt)
    #但是重写其他的一切
    重写规则^(.*)$index.php/$1[L]
    #如果我们没有安装mod_rewrite,所有404
    #可以发送到index.php,一切正常。
    ErrorDocument 404 index.php
    
  • 打开模式_重写(或)

  • 重新启动服务器

  • 这将把所有域/登录请求转发到前端控制器(index.php)。行重写第二个$1^(参考资料|系统|应用程序|扩展)将允许您确保某些文件夹不会被重写。因此,如果您在应用程序下有一个名为“resources”的文件夹,而不是将其转发到domain/index.php/resources,它将直接转到domain/resources

    实际上,如果没有.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> 
    
  • 检查URI中的域/前端控制器/路由模式,查看路由是否存在并适当转发
  • 通过域/登录,您没有遵循该模式,因此交付了404。将front_控制器(index.php)添加到URI使其遵循路由模式并转发到路由配置

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

    明白了

  • 实际定义

    base\u url='mysite.com'

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

    作为rest,所有URL调用都必须作为

    domain/index.php/controller/function/arg

  • 正如@Bill Garrison和开发者所建议的,应该在
    .htaccess
    中编写规则,从域名中删除index.php,然后路由器的url将正常工作


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

    起初我确信你一定是在什么地方打错了。然后我进入我的代码,以同样的方式创建了一条路由。宾果,我得到了同样的404。我将尝试调试这个。可能是最新版本的路由器代码中的错误。在那之前,我一直在思考这个问题。我解决了这个问题。答案如下。感谢您的回复。可以真管用!但问题仍然存在,如果我们在
    config.php
    中已经有
    $config['base\u url']='domain'
    $config['index\u page']='index.php'
    ,那么为什么需要对mod_rewrite进行显式更改呢?这是两种不同的方法:使用mod_rewrite时,跳过index.php,而在配置中,它实际上应该是空的。缺点是基本应用程序目录中的任何文件夹都必须在mod_重写中声明。这在我看来更安全。当应用程序在URI中看到$config['index_page']config时,不需要mod_rewrite,它知道之后会将信息转发到路由器。在配置中更改此选项只会告诉应用程序寻找不同的前端控制器。。。我将编辑我的回复,以便更好地解释。。另一种方法是什么,通过PHP本身而不触及.htaccess文件,我的意思是一定有某种方法,毕竟我使用的是一个框架:\url可以是
    域/控制器/方法
    !路由规则只是将
    domain/login
    转发到
    controller->method()
    yes…据我所知…应用程序将URI发送到路由器的唯一时间是当URI中声明的控制器/方法不存在时。