0

I am trying to build a very simple web app using codeigniter, for now I am trying to build static pages for both the website and the admin area.

but any routing is giving

404 page

except the home controller.

I have 2 main folders containing the web pages, one is for the user

application/views/pages/home.php

and the other is for the admin.

application/views/admin/dashboard.php

I can access the home page, but I can't access the admin page.

here is the Pages controller:

<?php
class Pages extends CI_Controller {
  public function view($page = 'home')
  {
    $this->load->helper('html');
          if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
          {
             show_404();
          }

          $data['title'] = ucfirst($page); // Capitalize the first letter
          $this->load->view('templates/header', $data);
          $this->load->view('pages/'.$page, $data);
          $this->load->view('templates/footer', $data);
  }
}
?>

and here is the admin controller

<?php
class Admin extends CI_Controller {
  public function view($admin = 'dashboard')
  {
          if ( ! file_exists(APPPATH.'views/admin/'.$admin.'.php'))
          {
                show_404();
          }
          $data['title'] = ucfirst($page);/* should be there*/
          $this->load->view('admin/'.$admin, $data);
  }
}
?>

and here is the routes

$route['admin'] = 'admin/dashboard';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['translate_uri_dashes'] = FALSE;

I can access the home page, but I can't access the admin page. any help, please?

1
  • 1
    change $route['admin'] = 'admin/dashboard'; to $route['admin'] = 'admin/view'; Commented May 15, 2018 at 12:52

2 Answers 2

1

Hope this will help you :

Make sure you have loaded url helper and .htaccess and set base_url in config.php

Your config.php

$config['base_url'] = 'http://localhost/project_folder/';
$config['index_page'] = '';

In autoload.php

$autoload['helper'] = array('url');

.htaccess file should be like this :

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

place .htaccess file in your project_folder

Replace

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

with this :

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

route.php should be like this :

$route['admin'] = 'admin/view';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['translate_uri_dashes'] = FALSE;

Access url like this :

http://localhost/project_folder/
http://localhost/project_folder/admin

For more : https://www.codeigniter.com/user_guide/tutorial/static_pages.html

Sign up to request clarification or add additional context in comments.

4 Comments

well, the same result, I did not create any .htaccess, but it's still giving Not Found
see my updated answer , just do like this and see you u get the result
ok, now it's not giving 404, but it's giving No input file specified.
ok, working now, I had to add ? sign to this rule before RewriteRule ^(.*)$ index.php/$1 [L,QSA] after RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
0

i think code should like this..theres an error at your if part i've corrected it hope this is work for you.....

<?php
class Admin extends CI_Controller {
  public function view($admin = 'dashboard')
  {
          if ( ! file_exists(APPPATH.'views/admin/'.$admin.'.php'))
          {
                show_404();
          }else{
           $data['title'] = ucfirst($page);/* should be there*/
          $this->load->view('admin/'.$admin, $data);
          }

  }
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.