简体   繁体   English

Codeigniter控制器和路由

[英]Codeigniter Controller and Routing

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. 我正在尝试使用codeigniter构建一个非常简单的Web应用程序,目前,我正在尝试为网站和管理区域构建静态页面。

but any routing is giving 但任何路由都给

404 page 404页

except the home controller. 除了家庭控制器。

I have 2 main folders containing the web pages, one is for the user 我有2个包含网页的主文件夹,其中1个供用户使用

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: 这是Pages控制器:

<?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? 有什么帮助吗?

Hope this will help you : 希望这个能对您有所帮助 :

Make sure you have loaded url helper and .htaccess and set base_url in config.php 确保您已加载url helper和.htaccess并在config.php中设置base_url

Your config.php 您的config.php

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

In autoload.php autoload.php中

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

.htaccess file should be like this : .htaccess文件应如下所示:

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 .htaccess文件放置在project_folder中

Replace 更换

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

with this : 有了这个 :

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

route.php should be like this : route.php应该是这样的:

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

Access url like this : 像这样访问URL:

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

For more : https://www.codeigniter.com/user_guide/tutorial/static_pages.html 有关更多信息: https : //www.codeigniter.com/user_guide/tutorial/static_pages.html

i think code should like this..theres an error at your if part i've corrected it hope this is work for you..... 我认为代码应该像这样..theres在您的错误如果我已经纠正它希望这对您有用.....

<?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);
          }

  }
}
?>

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

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