需要Laravel门面的澄清

问题描述 投票:1回答:1

因此,在laravel 5.2 laravel文档中也指出,这是门面如何在laravel实现。

<?php

namespace App\Http\Controllers;

use Cache;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    public function showProfile($id)
    {
        $user = Cache::get('user:'.$id);

        return view('profile', ['user' => $user]);
    }
}

我们能做到这一点呢?

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Cache;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    public function showProfile(Cache $cache, $id)
    {
        $user = $cache->get('user:'.$id);

        return view('profile', ['user' => $user]);
    }
}

从我所看到的我认为,

use Cache;

只是封装调用

Illuminate\Support\Facades\Cache

我对么?引导该命名空间成别名我相信应用程序?

更多的澄清,肯定会有所帮助。我是新来laravel.Anything我解释或描述错误请指正谢谢。

php laravel laravel-5 laravel-5.2
1个回答
1
投票

是的,我相信你可以,但我不会那样做。相反,考虑Laravel门面的目的:一类是能够坐在全局命名空间,它允许一个给定的实例的公共方法静态访问的。

Laravel外立面只不过是语法糖多了,我真的建议,以避免如果在所有可能使用它们。虽然很方便,他们往往会混淆代码车驾。

在缓存门面的情况下,可以基本弄清楚,就是BEING通过观察两个门面的代码,以及服务提供商负责所有缓存的运动部件的杠杆的实际类:

缓存门面

namespace Illuminate\Support\Facades;

/**
 * @see \Illuminate\Cache\CacheManager
 * @see \Illuminate\Cache\Repository
 */
class Cache extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'cache';
    }
}

您可以在评论中看到,有以\Illuminate\Cache\CacheManager\Illuminate\Cache\Repository参考。因此,这是什么呢?这里的关键是门面访问cache。如果我们看一下服务提供商,我们可以看到使用这个访问时,由外立面返回什么类:

缓存服务提供商

class CacheServiceProvider extends ServiceProvider
{
    ...
    public function register()
    {
        $this->app->singleton('cache', function ($app) {
            return new CacheManager($app);
        });

        $this->app->singleton('cache.store', function ($app) {
            return $app['cache']->driver();
        });

        $this->app->singleton('memcached.connector', function () {
            return new MemcachedConnector;
        });

        $this->registerCommands();
    }
    ...
}

我建议,宣布你这样的依赖性:

<?php

namespace App\Http\Controllers;

use Illuminate\Cache\CacheManager as Cache;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    public function showProfile(Cache $cache, $id)
    {
        $user = $cache->get('user:'.$id);

        return view('profile', ['user' => $user]);
    }
}

我相当肯定的是,PHP实际上允许您从一个实例访问静态方法。如果是这样的话,那么做你所想的需要看起来像这样的方式的方式:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Cache;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    public function showProfile(Cache $cacheFacade, $id)
    {
        $cache = $cacheFacade->getFacadeRoot()
        $user = $cache->get('user:'.$id);

        return view('profile', ['user' => $user]);
    }
}

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.