CodeIgniter不使用控制器和视图的多级子文件夹

我在Codeigniter中使用智能模板。</ p>

问题是,如果 我在视图或控制器中查找第二个子文件夹,Codeigniter停止工作... </ p>

例如</ p>

  application / controllers / main。  php  -  works 
application / controllers / admin / dashboard.php - works
application / controllers / admin / manageUsers / ListUsers.php - not working
</ code> </ pre>

我搜索过 网络,他们说我可以使用路由,这可能与控制器一起工作.. </ p>

但它是我关注的观点..
i意味着管理员的意见应该去 在管理员文件夹下,但我无法在管理员中创建子文件夹,如果我将所有视图放在管理员文件夹中,它将是一团糟,没有任何组织。 我希望你理解我想说的话。</ p>

例如</ p>

  themes / default / views / home.tpl  -  works \  nthemes / default / views / admin / dashboard.tpl工作
themes / default / views / admin / site_settings / preferences.tpl无法正常工作
</ code> </ pre>

请任何人指导我 如何解决这些问题。</ p>
</ div>

展开原文

原文

I am working in Codeigniter with smarty templates.

Problem is that if i go about 2nd subfolder in view or controller, the Codeigniter stops working...

e-g here

application/controllers/main.php  - works
application/controllers/admin/dashboard.php - works
application/controllers/admin/manageUsers/ListUsers.php - not working

I have searched the web, and they said that i can work with routes, which might do work with controller..

but it is views that i am concern of.. i mean admin views should go under admin folder, but i can not create subfolder in admin, if i put all views in admin folder, it will be a mess, nothing organized. i hope you are understanding what i am trying to say.

e-g

themes/default/views/home.tpl - works
themes/default/views/admin/dashboard.tpl works
themes/default/views/admin/site_settings/preferences.tpl not working

please can anyone guide me how to fix these issues.

drpogkqqi536984960
drpogkqqi536984960 您是否尝试编辑config/route.php文件并添加$route['admin/(.*)']='admin/$1';?另一种方法是添加一个Custom_Router,它扩展CI_Router并在那里更改require/include逻辑。你试过吗?
6 年多之前 回复
dongxin9759
dongxin9759 在您的管理控制器中,您是否正在执行功能管理($site_settings,$preferences);
6 年多之前 回复
duande9301
duande9301 你在使用模板库吗?视图文件的文件夹结构对我来说似乎很奇怪。
6 年多之前 回复

2个回答

Your problem is quite common. I've had it when I started working with CodeIgniter as well. What I found out to be the best way to overcome it, was to create a Custom_Router, which extends the CI_Router class. Doing that allows you to overwrite the controller class include/require logic and allow the usage of subdirs. This is a working example:

<?php

class Custom_Router extends CI_Router
{
    public function __construct()
    {
        parent::__construct();
    }

    public function _validate_request($segments)
    {
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.DIRECTORY_SEPARATOR.$segments[0]))
            {
                // Set the directory and remove it from the segment array
                $this->directory = $this->directory . $segments[0] .DIRECTORY_SEPARATOR;
                $segments = array_slice($segments, 1);
            }


            if (count($segments) > 0)
            {
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }

        show_404($segments[0]);
    }
}

?>

The code above works fine with as many subdirs as you'd want, although I wouldn't advice using this approach. I usually specifically state the path to my controllers in the route.php file.

Regarding your second problem - the templates. I never liked messy code and even messier viewers which contain <?php echo $something; for(...){}; foreach(){}... ?> all over the place. For me, that makes the code really hard to read and specially harder to debug. That's why I've been using the Twig template engine. There are tutorials how to get it working in CodeIgniter (I've used this one). Once you're done with it, in your controller you would simply need to write:

class Login extends Custom_Controller
{
    /**
     * Index Page for this controller.
     */
    public function index() {
        $data = array();
        $error_message = "Invalid user!";

        if($this->session->getUserId() != null){
            redirect('/welcome');
        }

        // other logic....



        // depending on how you configure twig, this will search for "login.html.twig"
        // in "application/views/". If your file is located somewhere in the subdirs, 
        // you just write the path: 
        //   admin/login.html.twig => application/views/admin/login.html.twig
        $this->twig->display('login.html.twig', $data); 
    }
}

If Twig is not an option for you, then you will need to create a new class which extends the CI_Loader class and overwrite the public function view(){} method.

By the way, if you're creating a web application with a backend, you might find it easier to manage and maintain your code if you separate your applications in different directories. If you choose to go this way, you will need to create application/public and application/admin folders preserving the directory structure of a CodeIgniter "application". Here are the steps:

  1. Create separate applications

    /applications
    -- public (a standard application directory structure)
    ---- cache
    ---- config
    ---- controllers
    ---- models
    ---- views
    ---- ...
    -- admin (a standard application directory structure)
    ---- cache
    ---- config
    ---- controllers
    ---- models
    ---- views
    ---- ...
    
  2. Open /index.php and change $application_folder to point to applications/public

  3. Create a copy of /index.php, name it backend.php (or whatever you want). Open the file and change $application_folder to point to the applications/admin folder.

  4. Open .htaccess and add a rule to pass all URI starting with /admin to backend.php

    # Route admin urls
    RewriteCond %{REQUEST_URI} admin([/])?(.*)
    RewriteRule .* backend.php?$1 [QSA,L]
    




  1. put 这个文件到application / core / </ li>
  2. 文件名必须是MY_Router.php </ li>
    </ ol >
    </ div>

展开原文

原文

  1. Put THIS file into application/core/
  2. Filename must be MY_Router.php

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问