【问题标题】:Codeigniter 3 Route not WorkingCodeigniter 3 路由不工作
【发布时间】:2017-03-09 17:09:47
【问题描述】:

我正在建立一个 CodeIgniter 项目,到目前为止一切进展顺利。我所有的路线和控制器都正常工作,除了一个。虽然我是 CodeIgniter 的新手,但我已经使用 PHP 进行了近五年的编程,所以我有一些经验。如果有人可以查看下面的代码示例并让我知道我做错了什么,那就太好了。

显然应用程序确实识别了路由,但它一直抱怨无法找到 URI。

控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Profile extends CI_Controller {
    function __construct() {
        parent::__construct();

        $this->load->helper('url');
    }

    public function Profile($page = 'profile-page')
    {
        if ( ! file_exists(APPPATH.'/views/'.$page.'.php'))
        {
            // Load error page
            show_404();
        }

        // Capitalize the first letter
        $data['title'] = ucfirst($page);
        $data['pageType'] = "profile-page";

        // Load pages
        $this->load->view('templates/header', $data);
        $this->load->view('templates/topbar', $data);
        $this->load->view('profile');
        $this->load->view('templates/footer', $data);
        //
    }
}

接下来是我的路线:

$route['default_controller'] = 'home/home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

// All custom routes go below this line.

$route['contact'] = 'pages/contact';
$route['signup'] = 'auth/signup';
$route['login'] = 'auth/login';
$route['profile'] = 'profile/profile';

我已验证所有视图文件都在正确的文件夹中,并且没有拼写错误。但是 Profile 路由一直失败。除 Profile 路由外,所有其他路由均正常工作。

我已经在带有 XAMMP 的 Windows 10、带有 MAMP 的 Mac OS X Sierra 和带有 Apache/PHP/MySQL 的 CentOS 7 上尝试过这个。

谢谢。

【问题讨论】:

    标签: php codeigniter routes mamp


    【解决方案1】:

    问题是您的代码进入了这种情况,它无法在该路径找到 profile-page.php 文件并显示错误。正如我检查的那样,配置文件函数正在正确调用而没有任何问题

    if ( ! file_exists(APPPATH.'/views/'.$page.'.php'))
            {
                echo "test";die;
                // Load error page
                show_404();
            }
    

    【讨论】:

    • 虽然不是我问题的答案,但您的问题确实为我指明了正确的方向。我得到它的工作,结果它是一个丢失的文件。
    【解决方案2】:

    我已经有一段时间没有编写 CodeIgniter 路由了,但我似乎记得方法名称是区分大小写的,所以大写“P”的 Profile 可能会导致问题?

    不过,我通常会去调试它的方法是尝试重命名方法,重命名控制器;这个想法是您可以确定问题是否出在路由中,或者代码中是否存在一些看不见的错字或其他问题,或者是否是路由本身

    不确定这是否有帮助?不过祝你好运!

    【讨论】:

    • 对不起 - 声明,我的意思是在 CodeIgniter 中编写路由,而不是试图声称我编写了 CodeIgniter 路由!
    【解决方案3】:

    Mac OSX 区分大小写,请确保控制器中的文件名与您在路由中尝试访问的大小写匹配。如果您也发布输出也会有所帮助。不久前我遇到了类似的问题,我通过根据定义的路由重命名文件名来解决这个问题。请参考我的回答 HMVC codeigniter works on local server but not on web server 在这里。

    【讨论】:

    • 我在 Windows 10 上遇到了同样的问题,微软不区分大小写。但我会尝试您的解决方案并让您知道。
    【解决方案4】:

    在默认控制器中,如果您使用 CI 3,则不能使用子文件夹,除非您修改了 MY_Router.php

    在 application/core/MY_Router.php 中

    这是原始问题here

    <?php
    
    class MY_Router extends CI_Router {
    
        protected function _set_default_controller() {
    
            if (empty($this->default_controller)) {
    
                show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
            }
    
            // Is the method being specified?
    
            if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
                $method = 'index';
            }
    
            // This is what I added, checks if the class is a directory
    
            if( is_dir(APPPATH.'controllers/'.$class) ) {
    
                // Set the class as the directory
    
                $this->set_directory($class);
    
                // $method is the class
    
                $class = $method;
    
                // Re check for slash if method has been set
    
                if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
                    $method = 'index';
                }
            }
    
    
            if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {
    
                // This will trigger 404 later
    
                return;
            }
    
            $this->set_class($class);
            $this->set_method($method);
    
            // Assign routed segments, index starting from 1
    
            $this->uri->rsegments = array(
                1 => $class,
                2 => $method
            );
    
            log_message('debug', 'No URI present. Default controller set.');
        }
    } 
    

    【讨论】:

    • 感谢您的回答。但是,我的所有路线都可以使用,除了个人资料路线。这是唯一行不通的路线。默认路由与列出的所有其他路由一样有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    相关资源
    最近更新 更多