dsqtl335227 2018-05-15 12:49
浏览 89
已采纳

Codeigniter控制器和路由

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?

  • 写回答

2条回答 默认 最新

  • dtgr6303 2018-05-15 13:05
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵