06.10.2016 Views

laravel-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Facades 206<br />

13 * @param int $id<br />

14 * @return Response<br />

15 */<br />

16 public function showProfile($id)<br />

17 {<br />

18 $user = Cache::get('user:'.$id);<br />

19<br />

20 return view('profile', ['user' => $user]);<br />

21 }<br />

22 }<br />

Notice that near the top of the file we are “importing” the Cache facade. This facade serves as a<br />

proxy to accessing the underlying implementation of the Illuminate\Contracts\Cache\Factory<br />

interface. Any calls we make using the facade will be passed to the underlying instance of Laravel’s<br />

cache service.<br />

If we look at that Illuminate\Support\Facades\Cache class, you’ll see that there is no static method<br />

get:<br />

1 class Cache extends Facade<br />

2 {<br />

3 /**<br />

4 * Get the registered name of the component.<br />

5 *<br />

6 * @return string<br />

7 */<br />

8 protected static function getFacadeAccessor() { return 'cache'; }<br />

9 }<br />

Instead, the Cache facade extends the base Facade class and defines the method getFacadeAccessor().<br />

Remember, this method’s job is to return the name of a service container binding. When a<br />

user references any static method on the Cache facade, Laravel resolves the cache binding from the<br />

service container and runs the requested method (in this case, get) against that object.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!