engageinteractive / laravel-frontend
Laravel项目的前端构建框架
Requires
- laravel/framework: >=5.5
Requires (Dev)
- mockery/mockery: ^1.1
- orchestra/testbench: ^3.6
README
Laravel包,为非生产环境提供前端模板路由。
安装
composer require engageinteractive/laravel-frontend
包的服务提供者在启动时将自动加载。
然后发布模板和配置文件
php artisan vendor:publish --provider="EngageInteractive\LaravelFrontend\ServiceProvider"
以这种方式发布的文件是结构示例,并不由包强制执行。编辑 config/frontend.php
以更改这些文件的路径。如果您还需要更改 config/frontend.php
的文件名,请参阅 配置文件自定义。
基本用法
将以下键添加到您的 .env
文件中,以启用前端路由(通常是本地和预发布)
FRONTEND_ENABLED=true
如果此键已用于您的项目,您可以在 config/frontend.php
文件中更改它。
现在您可以通过访问 /frontend/
来查看模板。
页面默认值
在应用程序中,通常很有用,当控制器没有明确提供时,可以从配置或数据库中加载回退变量。例如,在HTML <head>
中的页面标题就是这样。根据您的设置,在构建前端模板时,您可能没有定义数据库,或者您甚至可能不想涉及数据库。在这种情况下,您仍然希望布局模板接收这些变量,但对于所有前端模板,最好是将它们硬编码。
为此,您可以子类化 PageDefaultsViewComposer
并在服务提供者中注册它
子类化实现您自己值的视图生成器
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use EngageInteractive\LaravelFrontend\PageDefaultsViewComposer as BaseViewComposer;
class PageDefaultsViewComposer extends BaseViewComposer
{
/**
* Gets frontend default variables.
*
* @return array
*/
protected function defaultsForFrontend()
{
return [
'page' => [
'title' => 'HTML Meta Title',
'description' => 'HTML Meta Description',
...
],
];
}
/**
* Gets application default variables (i.e. ones used when not in the
* frontend templates.)
*
* @return array
*/
protected function defaultsForApp()
{
return [
'page' => [
'title' => config('app.name'),
...
],
];
}
}
注册您的视图生成器
<?php
namespace App\Http\ViewComposers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use App\Http\ViewComposers\PageDefaultsViewComposer;
class ViewComposerServiceProvider extends ServiceProvider
{
/**
* Register bindings in the container.
*
* @return void
*/
public function boot()
{
// Here the 'app/' directory is assumed to be all the individual pages,
// and does not contain partials, or layouts. This is because the
// composer will be ran multiple times if the Blade template extends
// from files also in the 'app/' directory.
View::composer('app/*', PageDefaultsViewComposer::class);
}
}
配置文件自定义
默认情况下,该包使用 config/frontend.php
文件来定义所有设置,例如路由名称、URL路径、模板文件路径等。然而,该包使用 Laravel Config Provider 允许您更改使用哪个文件。为此,在您的 AppServiceProvider
中绑定自己的 ConfigProvider
实例。这在例如 config/example-package.php
已在您的项目中使用的情况下非常有用。
首先创建您自己的提供者
namespace App\Config; use EngageInteractive\LaravelFrontend\ConfigProvider; class FrontendConfigProvider extends ConfigProvider { /** * Key to use when retrieving config values. * * @var string */ protected $configKey = 'laravel-frontend'; }
然后,在启动时添加提供者到绑定。
class AppServiceProvider extends ServiceProvider { ... /** * All of the container bindings that should be registered. * * @var array */ public $bindings = [ \EngageInteractive\LaravelFrontend\ConfigProvider::class => \App\Config\FrontendConfigProvider::class, ]; ... }
现在,在整个包中,当通过Laravel服务容器请求 ConfigProvider
时,您的将创建。
Laravel兼容性
适用于Laravel 5.5、5.6和5.7。
许可协议
Laravel Frontend是开源软件,许可协议为 MIT许可证。