简体   繁体   English

Codeigniter路由抛出404页面找不到错误

[英]Codeigniter routing throws 404 page not found error

When I am using url as 当我使用url时

mysite.com/index.php/user/user the method is getting called. mysite.com/index.php/user/user这个方法被调用了。

But when I add below line in routes.php so that I can access the function using custom url. 但是当我在routes.php添加以下行时,我可以使用自定义URL访问该函数。 it is throwing me error. 它给我带来了错误。 404 Not Found

I want to access the method using url as mysite.com/user 我想使用url作为mysite.com/user访问该方法

$route['user']['get'] = 'user/user';

user.php is present inside controller directory user.php存在于控制器目录中

    <?php
    use Restserver\Libraries\REST_Controller;
    defined('BASEPATH') OR exit('No direct script access allowed');

    require APPPATH . 'libraries/REST_Controller.php';
    require APPPATH . 'libraries/Format.php';

    class User extends REST_Controller {

        function __construct()
        {
            // Construct the parent class
            parent::__construct();
        }

        public function user_get(){
            $this->set_response("request recieved", REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
        }
    }

[2nd question] [第二个问题]

how to make a route to access the controller present inside certain folders in controller directory. 如何创建一个路径来访问控制器目录中某些文件夹中的控制器。

suppose a controller file is present in controllers/api/v1/ directory with file name user.php 假设控制器文件存在于controllers/api/v1/目录中,文件名为user.php

Note: I have tried all the solutions given by users on other posts but issue was not resolved. 注意:我已尝试过用户在其他帖子上提供的所有解决方案,但问题未得到解决。


EDIT: Issue Resloved 编辑:问题重新开始

Routing is working just fine now. 路由工作正常。 I think the problem was, I was calling mysite.com/user , instead I should have called mysite.com/index.php/user 我认为问题是,我在调用mysite.com/user,而不是我应该调用mysite.com/index.php/user

Second issue of index.php being in the url is also resolved. 第二期index.php在url中也得到了解决。 I was making the changes in .htaccess file which was present in Application folder instead. 我在.htaccess文件中进行了更改,而该文件存在于Application文件夹中。 I created a .htaccess file in root folder then added below mentioned line of code 我在root folder创建了一个.htaccess文件,然后添加了下面提到的代码行

 <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L] </IfModule> 

You have set your route as 您已将路线设置为

$route['user']['get'] = 'user/user';

Which is directing to the user controller and the user method. 这是指向用户控制器和用户方法。 But in your controller you have a method called user_get . 但是在你的控制器中你有一个名为user_get的方法。

So the simplest fix here is to change your route to point to the correct method. 所以这里最简单的解决方法是改变您的路线以指向正确的方法。 NOTE: You have no method called 'user' so why is that in the route? 注意:您没有名为'user'的方法,那为什么在路线中呢?

So this... 所以这...

$route['user']['get'] = 'user/user'; // the user method does not exist

Would become... 会成为...

$route['user']['get'] = 'user/user_get'; // the user_get method does exist

Update: To remove index.php from your URL, your .htaccess might be 更新:要从您的URL中删除index.php,您的.htaccess可能是

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

And make sure you add in your base_url in applications/config/config.php 并确保在applications / config / config.php中添加base_url

$config['base_url'] = 'http://example.com';  // Change to your sites URL
$config['index_page'] = '';  // remove index.php                

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM