php Phalcon注册命名空间

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Phalcon注册命名空间相关的知识,希望对你有一定的参考价值。

<?php
namespace App\Assets\JS;

class ApplicationManifest
{
    protected $assets;

    public function __construct($assets)
    {
        $assets->collection('fullcalendarCSS')
               ->addCss('node_modules/fullcalendar/dist/fullcalendar.min.css')
               ->join(true)
               ->addFilter(
                    new Phalcon\Assets\Filters\Jsmin()
                )
               ->setTargetPath('public/application.css');
        $assets->collection('fullcalendarJS')
               ->addJs('node_modules/jquery/dist/jquery.min.js')
               ->addJs('node_modules/moment/min/moment.min.js')
               ->addJs('node_modules/fullcalendar/dist/fullcalendar.min.js')
               ->addJs('js/fullcalendarImpl.js')
               ->join(true)
               ->setTargetPath('public/application.js');
    }
}

<?php

use Phalcon\Loader;
use Phalcon\Tag;
use Phalcon\Mvc\Url;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Mvc\View\Engine\Volt as PhVolt;
use Phalcon\DI\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;

try {

    // Register an autoloader
    $loader = new Loader();
    $loader->registerNamespaces(
        [
           'App\Assets\JS'    => '../app/assets/js/'
        ]
    );
    $loader->registerDirs(
        array(
            '../app/controllers/',
            '../app/models/'
        )
    );
    $loader->register();

    // Create a DI
    $di = new FactoryDefault();
    
    $di->set(
        'assetPipeline',
        [
            'className' => 'App\Assets\JS\ApplicationManifest',
            'arguments' => [[
                'type' => 'service',
                'name' => 'assets',
            ]]
        ]
    );

    new App\Assets\JS\ApplicationManifest();

    echo $di['assetPipeline'];

    // Set the database service
    $di['db'] = function() {
        return new DbAdapter(array(
            "host"     => "localhost",
            "username" => "root",
            "password" => "secret",
            "dbname"   => "tutorial"
        ));
    };

    // Setting up the view component
    $di['view'] = function() use ($di) {
        $options  = [
            'compiledPath'      => '../storage/cache/volt/',   //path of where your templates will be compiled
            'compiledSeparator' => '_',
            'compiledExtension' => '.php',
            'compileAlways'     => true,
            'stat'              => true,
        ];
        $view = new View();
        $view->setViewsDir('../app/views/');
        $view->registerEngines(
        [
            '.volt' => function ($view) use ($options, $di) {
                $volt  = new PhVolt($view, $di);
                $volt->setOptions($options);
                return $volt;
            },
        ]
    );
        return $view;
    };

    $di['url']->setBaseUri('/');

    // Handle the request
    $application = new Application($di);
    echo $application->handle()->getContent();

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

以上是关于php Phalcon注册命名空间的主要内容,如果未能解决你的问题,请参考以下文章

PHP 命名空间与 Phalcon、Vagrant、Apache2 的冲突

phalcon: router规则与解析,已经生成router的链接地址

phalcon:数据库分库,读写分离,负载均衡 系统方法执行顺序

php自动加载带命名空间类的函数

学习phalcon框架按照官网手册搭建第一个项目注册功能

php设计模式 工厂单例注册树模式