Tim C
Tim C

Reputation: 5714

CodeIgniter Route Not Found

Im learning CodeIgniter, thus I am still very new to the platform. I have the following problem:

I created a controller named admin;

I then added the path inside the routes file as such:

$route[admin/dashboard] = 'admin/dashboard;

Controller

class Admin extends CI_Controller {

    public function dashboard($page ='dashboard'){

        if(!file_exists(APPPATH.'/views/pages/'.$page.'.php')){
            echo 'error';
            show_404();
        }
        //check if Admin
        $data['isAdmin'] = $this->admin_model->isAdmin($this->session->userID);
        var_dump($data['isAdmin']);
        $data['title'] = $page;
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
    }

routes.php

  #ADMIN

    $route['admin/dashboard'] = 'admin/dashboard'; //ROUTE FOR ADMIN DASHBOARD

    #USERS
    $route['users']= 'users';
    $route['users/index'] = 'users/index';
    $route['users/login'] ='users/login';
    $route['users/dashboard'] ='users/dashboard';
    $route['users/profile'] = 'users/profile';
    $route['users/userpicks/(:any)'] = 'users/userpicks/$1';

    #PAGES
    $route['pages/index'] = 'pages/index';
    $route['pages/user_data_submit'] = 'pages/user_data_submit';
    $route['(:any)'] = 'pages/index/$1';
    $route['default_controller'] = 'pages/index';

Views

admin
 ------- dashboard.php

Config.php

$config['base_url'] = 'http://mysite';

PROBLEM / QUESTION

When I try to access http://mysite/admin/dashboard I get a 404 error

Tracing the route gives the following problem which I find strange. Looking at the below I believe the problem is with the configuration of routes.php ...?

DEBUG - 2018-06-26 08:53:45 --> UTF-8 Support Enabled DEBUG -

2018-06-26 08:53:45 --> Client sent : dashboard DEBUG - 2018-06-26

08:53:45 --> Route found : (:any) --> pages/index/$1 DEBUG -

2018-06-26 08:53:45 --> Redirecting to : dashboard -->

pages/index/dashboard DEBUG - 2018-06-26 08:53:45 --> Global POST, GET

and COOKIE data sanitized DEBUG - 2018-06-26 08:53:45 --> Session:

"sess_save_path" is empty; using "session.save_path" value from

php.ini. ERROR - 2018-06-26 08:53:45 --> 404 Page Not Found:

By looking at the above http://mysite/admin/dashboard somehow gets redirected to

Route found : (:any) --> pages/index/$1 DEBUG

I find this strange as I have "hardcoded" the route and placed it at the very top of routes.php yet it doesn't get executed for some reason...?

Any input and / or help appreciated.

Upvotes: 2

Views: 9091

Answers (3)

F5 Buddy
F5 Buddy

Reputation: 494

In your route file your should remove

$config['index_page'] = 'index.php';
  To 
$config['index_page'] = '';

.htaccess File

And create a .htaccess file on your project root folder

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

this .htaccess file code will remove index.php from url

Upvotes: 2

Neeraj
Neeraj

Reputation: 702

For simplicity remove routes you define, There is no need to define routes in CI. If you have a controller "admin" and an action dashboard, you can simply browse it with URL

http://yoursite/admin/dashboard

. If index.php is not removed by you explicitly then URL will be

http://yoursite/index.php/admin/dashboard

. If you are on a local machine like XAMP then URL will be

http://localhost/folderName//index.php/admin/dashboard

.

Hope it will help

Upvotes: 1

Sushant Bassi
Sushant Bassi

Reputation: 379

  $route['admin'] = "admin/dashboard";

    $route['default_controller'] = "pages/index"; 

(This means the path is controller->pages->index.php) Just confirming it to you.

One more case it might be due to the htacess.php file

Change the route to :

http://mysite/index.php/admin/dashboard

Upvotes: 0

Related Questions