沃梦达 / 编程问答 / php问题 / 正文

如何在 Laravel 4 中创建自定义外观

How to create custom Facade in Laravel 4(如何在 Laravel 4 中创建自定义外观)

本文介绍了如何在 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).

Settings::get(), Settings::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.

谢谢,克里斯

寻找一步一步的简单解释如何和为什么.

Looking for a step by step with simple explanations of how and why.

推荐答案

首先你需要进入 app/config/app.php 并在 providers 部分添加:

First you need to go to app/config/app.php and in providers section add:

'LaracmsProvidersSettingsServiceProvider',

您应该在 aliases 部分的同一文件中添加:

In the same file in aliases section you should add:

 'Settings' => 'LaracmsFacadesSettings',

在你的 app/Laracms/Providers 你应该创建文件 SettingsServiceProvider.php

In your app/Laracms/Providers you should create file SettingsServiceProvider.php

<?php

namespace LaracmsProviders;

use IlluminateSupportServiceProvider;

class SettingsServiceProvider extends ServiceProvider {

    public function register()
    {
        $this->app->bind('settings', function()
            {
                return new LaracmsSettings();
            });
    }

}

在你的 app/Laracms/Facades/ 你应该创建文件 Settings.php:

In your app/Laracms/Facades/ you should create file Settings.php:

<?php

namespace LaracmsFacades;

use IlluminateSupportFacadesFacade;

class Settings extends Facade {

    protected static function getFacadeAccessor() { return 'settings'; }

}

现在在您的 app/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"
    ]
},

现在您需要在项目文件夹中的控制台中运行:

Now you need to run in your console inside your project foler:

composer dump-autoload

创建类映射

如果一切正常,您现在应该可以在应用程序中使用 Settings::get()Settings: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 中创建自定义外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 Laravel 4 中创建自定义外观

基础教程推荐