CodeIgniter不能使用控制器和视图的多级子文件夹 [英] CodeIgniter not working with multiple level subfolders for both controllers and views

查看:140
本文介绍了CodeIgniter不能使用控制器和视图的多级子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用smarty模板在Codeigniter工作。



问题是,如果我去看第二个子文件夹的视图或控制器,Codeigniter停止工作...

例如这里

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

我已经在网上搜索,他们说我可以使用路由,这可能与控制器工作。



它是我的关注的意见..
i意味着管理视图应该在管理文件夹下,但我不能在管理中创建子文件夹,如果我把所有视图在管理文件夹,这将是一个混乱,没有组织。我希望你理解我想说的话。



例如

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

请任何人指导我如何解决这些问题。

解决方案

您的问题很常见。我已经有了,当我开始使用CodeIgniter以及。我发现是克服它的最好方法是创建一个 Custom_Router ,它扩展了 CI_Router 类。这样做可以覆盖控制器类include / require逻辑并允许使用subdirs。这是一个工作示例:

 <?php 

类Custom_Router扩展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]))
{
//设置目录并从段数组
$ this-> directory = $ this->中删除它。 $ 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]);
}
}

?>

上面的代码适用于许多子目录,使用这种方法。我通常在 route.php 文件中明确指出我的控制器的路径。



关于您的第二个问题 - 模板。我从来不喜欢凌乱的代码,甚至包含<?php echo $ something;对于(...){}; foreach(){} ...?> 遍历整个地方。对我来说,这使得代码真的很难阅读,特别难以调试。这就是为什么我一直在使用 Twig 模板引擎。有教程如何让它工作在CodeIgniter(我使用了这个)。一旦你完成它,在你的控制器,你只需要写:

  class Login extends Custom_Controller 
{
/ **
*此控制器的索引页。
* /
public function index(){
$ data = array();
$ error_message =无效的用户!

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

//其他逻辑....



//根据你如何配置twig,这将搜索login.html.twig
// inapplication / views /。如果你的文件位于子目录
//的某个位置,你只需要写下路径:
// admin / login.html.twig => application / views / admin / login.html.twig
$ this-> twig-> display('login.html.twig',$ data);
}
}

如果Twig不是您的选择,将需要创建一个扩展 CI_Loader 类并覆盖 public function view(){} 方法的新类。



顺便说一句,如果您正在使用后端创建Web应用程序,则可能会发现如果您在不同目录中分离应用程序,则可以更轻松地管理和维护代码。如果你选择这样做,你将需要创建 application / public application / admin CodeIgniter应用程序的结构。以下是步骤:


  1. 创建单独的应用程序

      / applications 
    - public(标准应用程序目录结构)
    ----缓存
    ---- config
    ---- controllers
    ---- models
    ---- views
    ---- ...
    - admin(标准应用程序目录结构)
    --- - cache
    ---- config
    ---- controllers
    ----模型
    ---- views
    ---- ...


  2. 打开 /index.php 更改 $ application_folder 以指向 applications / public


  3. 创建 /index.php 的副本,将它命名为 backend.php (或任何您想要的)。打开文件并更改 $ application_folder 以指向 applications / admin 文件夹。


  4. 打开 .htaccess 并添加规则以传递以 / admin 到 backend.php

     #路由管理员urls 
    RewriteCond%{REQUEST_URI} admin([/])?(.*)
    RewriteRule。* backend.php?$ 1 [QSA,L]



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.

解决方案

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]
    

这篇关于CodeIgniter不能使用控制器和视图的多级子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆