如何创建自定义门面在Laravel 4 [英] How to create custom Facade in Laravel 4
问题描述
在外墙和laravel 4上查看了几个教程...尝试了一些...不喜欢他们的工作方式。
Looked up a few tutorials on facades and laravel 4... tried some... not liked the way they work.
例如,他们不所有提供一种方式来定义在哪里存储立面文件和服务提供商...我试图离开那里,让我的头撞到几个墙,直到我决定做这个线程。
For instance, they don't all provide a way of defining where to store the facade files and service providers... and i tried to step away from that and got my head bumped into a few walls until i decided to do this thread.
所以:假设我有一个应用程序叫Laracms(laravel cms)。
So: Let's say i have an app called Laracms (laravel cms).
我想将我创建的所有内容 - 外墙,服务提供商等存储在名为laracms的应用程序下的文件夹中。
I'd like to store everything i create - facades, service providers, etc in a folder under app named laracms.
所以我会有/ app / laracms / facades,/ app / laracms / serviceproviders等等。我不想将外观与数据库模型混合,我想尽可能保持独立。
So i'd have /app/laracms/facades, /app/laracms/serviceproviders and so on. I don't want to mix the facades with the database models, i want to keep things as separate as possible.
现在让我们来看一下设置名称对于立面(我想实现一个设置类使用视图和管理员设置杂项。)
Let's take now, in my case, the Settings name for the facade (i want to implement a settings class to use in views and admin to set up misc. stuff).
设置:: get(),设置: set()作为方法。
Settings::get(), Settings::set() as methods.
任何人都可以解释如何正确设置外观?我不知道我做错了,我需要一个新的开始。
Can anyone explain how to set facades up correctly? I don't know what i'm doing wrong and i need a fresh start.
感谢您,
Chris
Thanks, Chris
寻找一步一步的简单解释。
Looking for a step by step with simple explanations of how and why.
推荐答案
首先,您需要转到 app / config / app.php
和提供程序
部分添加:
First you need to go to app/config/app.php
and in providers
section add:
'Laracms\Providers\SettingsServiceProvider',
在别名
section您应该添加:
In the same file in aliases
section you should add:
'Settings' => 'Laracms\Facades\Settings',
在您的应用程式/ Laracms / Providers
,您应该创建文件 SettingsServiceProvider.php
In your app/Laracms/Providers
you should create file SettingsServiceProvider.php
<?php
namespace Laracms\Providers;
use Illuminate\Support\ServiceProvider;
class SettingsServiceProvider extends ServiceProvider {
public function register()
{
$this->app->bind('settings', function()
{
return new \Laracms\Settings();
});
}
}
> app / Laracms / Facades / 您应该创建文件 Settings.php
:
In your app/Laracms/Facades/
you should create file Settings.php
:
<?php
namespace Laracms\Facades;
use Illuminate\Support\Facades\Facade;
class Settings extends Facade {
protected static function getFacadeAccessor() { return 'settings'; }
}
现在在 Laracms
目录您应该创建文件 Settings.php
:
Now in your app/Laracms
directory you should create file Settings.php
:
<?php
namespace Laracms;
class Settings {
public function get() {echo "get"; }
public function set() {echo "set"; }
}
由于您希望自定义文件夹中的文件 Laracms
您需要将此文件夹添加到您的 composer.json
(如果您使用标准 app / models
文件夹,你不需要添加任何东西到这个文件)。所以现在打开 composer.json
文件和 autoload
- > classmap
您应该添加 app / Laracms
,因此composer.json的这一部分可能如下所示:
As you wanted to have your files in custom folder Laracms
you need to add this folder to your composer.json
(If you used standard app/models
folder you wouldn't need to add anything to this file). So now open composer.json
file and in section autoload
-> classmap
you should add app/Laracms
so this section of composer.json could look like this:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/Laracms"
]
},
现在您需要在您的控制台foler:
Now you need to run in your console inside your project foler:
composer dump-autoload
创建类映射
如果一切正常,您现在应该可以在您的应用程序中使用 :: get()
和设置:set()
If everything is fine, you should now be able to use in your applications Settings::get()
and Settings:set()
我使用带大写字母的文件夹,因为按照惯例,命名空间以大写字母开头。
You need to notice that I used folders with uppercases because namespaces by convention starts with upper letters.
这篇关于如何创建自定义门面在Laravel 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!