dsfovbm931034814 2017-09-26 22:45
浏览 21
已采纳

如何使用路由仅使用文件夹名称访问控制器

How to access controller only with folder name, folder exist in controller. Folder name is admin inside controller folder. Controller is dashboard(php file) inside admin folder.

$route['admin/(:any)']  = 'admin/dashboard';

htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]

I want to access dashboard(php file) using: http://Website/admin. But not working.I thnik, I have issue with above route line. enter image description here

Working route

$route['(:any)'] = "admin/dashboard";

Using $route['(:any)'] page can access with: http://Website/any-string-or-letter. But how to access only with: http://Website/admin.

  • 写回答

2条回答 默认 最新

  • doujinyi1267 2017-09-27 02:38
    关注

    Follow these steps to do this:

    1. Create a folder inside controllers folder application/controllers/admin
    2. Create a controller inside your folder. For Example: application/controllers/admin/dashboard

    Dashboard.php

    <?php
    
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Dashboard extends CI_Controller {
    
        /**
         * Index Page for this controller.
         *
         */
    
    
        public function index()
        {
            $this->load->view('my-view');
            //$this->load->view('admin/my-view'); //If you are saving the views in a sub directory under views/admin/my-view.php
        }
    
    }
    

    Create a my-view.php under application/controllers/views/my-view.php

    <h5>MY VIEW FILE CONTENT</h5>
    

    Access this controller like this: If you are on localhost then it will be like: http://localhost/my-project/admin/dashboard/index

    .htaccess

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /my-project
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
        RewriteRule ^(.*?)$ /my-project/index.php?/$1 [L]
    </IfModule>
    
    • *update your project folder name.

    routes.php

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

    *Please try to remove/comment all other routes except this one to avoid the conflicts

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

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM