Phalcon框架目录结构

phalcon框架默认mvc结构如下

 phalcon_test/
 |----app  //应用目录
 |      |----config  //配置目录
 |      |      |----config.php
 |      |      |----loader.php
 |      |      |----services.php
 |      |----controllers  //控制器目录
 |      |      |----ControllerBase.php
 |      |      |----IndexController.php
 |      |----library  //类库目录
 |      |----migrations  //数据库迁移文件目录
 |      |----models   //数据库模型目录
 |      |----views  //视图目录
 |      |      |----index
 |      |      |      |----index.volt
 |      |      |----index.volt
 |      |      |----layouts
 |----cache   //缓存目录
 |----index.html
 |----public  //入口目录
 |      |----.htaccess
 |      |----index.php  //入口文件
 |      |----css
 |      |----files
 |      |----img
 |      |----js
 |      |----temp
 |----.htrouter.php
 |----.htaccess

注:phalcon框架的目录的结构可以随自己的情况自定义。

分析默认结构中的主入口文件:phalcon_test -> public -> index.php

getConfig();    

/**    
 * 创建自动加载器 $loader、注册自动加载目录   
 */    
include APP_PATH . '/config/loader.php';    

/**     
 * 创建程序应用
 * 发送响应的内容
 */    
  $application = new \Phalcon\Mvc\Application($di); 
  echo $application->handle()->getContent();

} catch (\Exception $e) {    
  /**
   * 捕捉错误信息
   */
  echo $e->getMessage() . '
'; echo '
' . $e->getTraceAsString() . '
'; }

下面我们来调整下项目目录结构:

 phalcon_test/
 |----apps
 |      |----front
 |      |      |----controllers
 |      |      |      |----IndexController.php
 |      |      |----models
 |      |      |----views
 |      |      |      |----index
 |      |      |      |      |----index.phtml
 |----public
 |      |----.htaccess
 |      |----index.php
 |      |----css
 |      |----files
 |      |----img
 |      |----js
 |      |----temp
 |----.htrouter.php
 |----.htaccess

调整框架主入口文件:phalcon_test -> public -> index.php

registerNamespaces([        
'Front\Controllers' => '../apps/front/controllers/',        
'Front\Models'      => '../apps/front/models/',    
])->register();    

 //创建服务容器并注册服务    
 $di = new FactoryDefault();    

 //使用命名空间一定要设置派遣器
 $di->set('dispatcher', function () {        
      $dispatcher = new Dispatcher();        
      $dispatcher->setDefaultNamespace('Front\Controllers\\');        
      return $dispatcher;    
 });  
 
 //设置视图目录
 $di->set('view', function(){        
      $view = new View();        
      $view->setDI($this);        
      $view->setViewsDir('../apps/front/views/');        
      return $view;   
 });  

//创建程序应用,并发送响应内容   
$app = new Application($di);    
echo $app->handle()->getContent();
}catch (Exception $e){    
echo $e->getMessage();
}

默认主控制器:phalcon_test -> apps -> front -> controllers -> IndexController.php

namespace Front\Controllers;
use Phalcon\Mvc\Controller;
class IndexController extends Controller{    
public function indexAction()    {        
  $this->view->test='hello flycorn...';    
}}

默认主视图:phalcon_test -> apps -> front -> views -> index -> index.phtml




    
    
    
    Document


    

注:如果调整完目录结构无法访问项目,请重启服务,如果访问任何控制器都显示的是index默认的视图,请检查路由重写规则!


定制一个多模块的框架结构(前后台模块)

phalcon_test/
     |----apps
     |      |----frontend  //前台
     |      |      |----controllers
     |      |      |      |----IndexController.php
     |      |      |----views
     |      |      |      |----index
     |      |      |      |      |----index.phtml
     |      |      |----Module.php  //前台模块
     |      |----backend  //后台
     |      |      |----controllers
     |      |      |      |----IndexController.php
     |      |      |----views
     |      |      |      |----index
     |      |      |      |      |----index.phtml
     |      |      |----Module.php  //后台模块
     |      |----models  //数据模型
     |      |----Bootstrap.php  //应用入口
     |----public
     |      |----.htaccess
     |      |----index.php  //项目入口
     |      |----css
     |      |----files
     |      |----img
     |      |----js
     |      |----temp
     |----.htrouter.php
     |----.htaccess

项目入口文件:phalcon_test -> public -> index.php

use Apps\Bootstrap;
include dirname(__DIR__).DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.'Bootstrap.php';
$bootstrap = new Bootstrap(); 
$bootstrap->run();

应用入口文件:phalcon_test -> apps -> Bootstrap.php

namespace Apps;
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Application;
use Phalcon\Mvc\Router;

class Bootstrap
{
    public function run()
    {
        try{
            /**
             * 创建容器
             */
            $di = new FactoryDefault();

            /**
             * 注册路由
             */
            $di->set('router', function () {

                $router = new Router();
                $router->setDefaultModule("frontend");

                //后台路由
                $router->add("/admin", array(
                    'module'     => 'backend',
                    'controller' => 1,
                    'action'     => 2,
                ));
                $router->add("/admin/:controller", array(
                    'module'     => 'backend',
                    'controller' => 1,
                    'action'     => 2,
                ));
                $router->add('/admin/:controller/:action', array(
                    'module'     => 'backend',
                    'controller' => 1,
                    'action'     => 2,
                ));

                //前台路由
                $router->add(':controller/:action', array(
                    'module'     => 'frontend',
                    'controller' => 1,
                    'action'     => 2,
                ));

                return $router;
            });

            /**
             * 创建应用
             */
            $app = new Application($di);

            /**
             * 注册模块
             */
            $app->registerModules(array(
                'frontend' => array(
                    'className' => 'Apps\Frontend\Module',
                    'path'      => '../apps/frontend/Module.php'
                ),
                'backend'  => array(
                    'className' => 'Apps\Backend\Module',
                    'path'      => '../apps/backend/Module.php'
                )
            ));

            //发送响应
            echo $app->handle()->getContent();

        }catch(Exception $e){
            echo $e->getMessage();
        }
    }
}

前台模块:phalcon_test -> apps -> frontend -> Module.php

namespace Apps\Frontend;
use Phalcon\Loader;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\View;
use Phalcon\Db\Adapter\Pdo\Mysql;

class Module{

    /**
     * 注册命名空间
     */
    public function registerAutoloaders()
    {
        $loader = new Loader();
        $loader->registerNamespaces(array(
            'Apps\Frontend\Controllers' => '../apps/frontend/controllers/',
            'Apps\Models'      => '../apps/models/',
        ));
        $loader->register();
    }

    /**
     * 注册服务
     * @param $di
     */
    public function registerServices($di)
    {
        //注册派遣器
        $di->set('dispatcher', function () {
            $dispatcher = new Dispatcher();
            $dispatcher->setDefaultNamespace('Apps\Frontend\Controllers\\');
            return $dispatcher;
        });

        //注册视图
        $di->set('view', function () {
            $view = new View();
            $view->setViewsDir('../apps/frontend/views/');
            return $view;
        });

        //注册数据库
        $di->set('db', function () {
            return new Mysql(
                [
                    "host"     => "localhost",
                    "username" => "root",
                    "password" => "123456",
                    "dbname"   => "phalcon_test"
                ]
            );
        });
    }
    
}

后台模块:phalcon_test -> apps -> backend -> Module.php

namespace Apps\Backend;
use Phalcon\Loader;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\View;
use Phalcon\Db\Adapter\Pdo\Mysql;

class Module{

    /**
     * 注册命名空间
     */
    public function registerAutoloaders()
    {
        $loader = new Loader();
        $loader->registerNamespaces(array(
            'Apps\Backend\Controllers' => '../apps/backend/controllers/',
            'Apps\Models'      => '../apps/models/',
        ));
        $loader->register();
    }

    /**
     * 注册服务
     * @param $di
     */
    public function registerServices($di)
    {
        //注册派遣器
        $di->set('dispatcher', function () {
            $dispatcher = new Dispatcher();
            $dispatcher->setDefaultNamespace('Apps\Backend\Controllers\\');
            return $dispatcher;
        });

        //注册视图
        $di->set('view', function () {
            $view = new View();
            $view->setViewsDir('../apps/backend/views/');
            return $view;
        });

        //注册数据库
        $di->set('db', function () {
            return new Mysql(
                [
                    "host"     => "localhost",
                    "username" => "root",
                    "password" => "123456",
                    "dbname"   => "phalcon_test"
                ]
            );
        });

    }

}

前台控制器:phalcon_test -> apps -> frontend -> controllers -> IndexController.php

namespace Apps\Frontend\Controllers;
use Phalcon\Mvc\Controller;
class IndexController extends Controller
{
    public function indexAction()
    {
        echo '前台模块';
    }
}

后台控制器:phalcon_test -> apps -> backend -> controllers -> IndexController.php

namespace Apps\Backend\Controllers;
use Phalcon\Mvc\Controller;
class IndexController extends Controller
{
    public function indexAction()
    {
        echo '后台模块';
    }
}

Github上也提供的各种phalcon的mvc结构,也可以按照自己的场景自定义。
Phalcon MVC

你可能感兴趣的:(Phalcon框架目录结构)