博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
laravel4通过控制视图模板路劲来动态切换主题
阅读量:7192 次
发布时间:2019-06-29

本文共 2695 字,大约阅读时间需要 8 分钟。

通过控制视图模板路劲来动态切换主题

App::before(function($request){    $paths =  Terminal::isMobile() ? array(__dir__.'/views/mobile') : array(__dir__.'/views/pc');    View::setFinder( new Illuminate\View\FileViewFinder(App::make('files'),$paths));});

首先说说为什么直接通过修改配置不行

public function registerViewFinder()    {        $this->app->bindShared('view.finder', function($app)        {            $paths = $app['config']['view.paths'];            return new FileViewFinder($app['files'], $paths);        });    }

这段是用于注册FileViewFinder服务的代码,为什么要说这个服务,因为我们的View服务是依赖他来获取视图路劲的

public function registerFactory()    {        $this->app->bindShared('view', function($app)        {            // Next we need to grab the engine resolver instance that will be used by the            // environment. The resolver will be used by an environment to get each of            // the various engine implementations such as plain PHP or Blade engine.            $resolver = $app['view.engine.resolver'];            $finder = $app['view.finder'];             $env = new Factory($resolver, $finder, $app['events']);            // We will also set the container instance on this view environment since the            // view composers may be classes registered in the container, which allows            // for great testable, flexible composers for the application developer.            $env->setContainer($app);            $env->share('app', $app);            return $env;        });    }

视图的注册是返回一个工厂类,之后直接通过LOC容器去获取FileViewFinder:$finder = $app['view.finder'];

在理解这段代码之前我们需要先理解bindShared和不同的bind有何不同

public function share(Closure $closure)    {        return function($container) use ($closure)        {            // We'll simply declare a static variable within the Closures and if it has            // not been set we will execute the given Closures to resolve this value            // and return it back to these consumers of the method as an instance.            static $object;            if (is_null($object))            {                $object = $closure($container);            }            return $object;        };    }    /**     * Bind a shared Closure into the container.     *     * @param  string    $abstract     * @param  \Closure  $closure     * @return void     */    public function bindShared($abstract, Closure $closure)    {        $this->bind($abstract, $this->share($closure), true);    }

从这里可以看出bindShared不仅会向LOC容器注册,而且还会执行闭包函数,直接获取返回对象,也就是说当我在使用View::make()的时候这个对象已经存在了,并且服务的注册是在App::before之前的

通过上面的分析我们可以知道,当我们在App::before事件中直接修改视图路劲时,我们的视图服务和FileViewFinder是不会重新实例化的,所以他们获取的view.paths还是最开始定义的,也就是说我们在使用视图服务时想要改变路劲就需要重新设置我们的FileViewFinder服务,也就是最上面看到的代码。

 

 

 

转载于:https://www.cnblogs.com/xiaodo0/p/4318145.html

你可能感兴趣的文章
如何配置struts+hibernate,基本使用方法
查看>>
《OpenStack云计算实战手册(第2版)》一2.7 租户间共享镜像
查看>>
熬夜并不值得程序员炫耀
查看>>
《思科数据中心I/O整合》一2.8 基于优先级的流量控制(PFC)
查看>>
Hadoop 这样业界顶级的大规模数据处理平台,均发现满足不了类似双十一这样全世界的剁手党蜂拥而至的热情...
查看>>
Kilim实现浅析(一)
查看>>
Maven入门指南(二)
查看>>
《万物互联》——2.9 从物联网中盈利
查看>>
《C语言接口与实现:创建可重用软件的技术》一导读
查看>>
Gartner最新发布:2017年十大战略技术趋势
查看>>
《21天学通C语言(第7版)》一2.4 小 结
查看>>
redis集群搭建
查看>>
浅析mybatis源码(二)连接池
查看>>
Zabbix企业微信告警最新版
查看>>
sqlite3 演示
查看>>
Python3.7源码安装
查看>>
从微软中国下载Windows系统并安装
查看>>
java session HttpSessionListener、HttpSessionBindingListener使用区别,实现在线人数统计以及踢出用户...
查看>>
Spring事务管理
查看>>
磁盘格式化、磁盘挂载、手动增加swap空间
查看>>