mikemclin/laravel-to-angular-constant

将 Laravel 数据输出为 Angular 常量

1.0.0 2015-08-09 16:30 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:55:40 UTC


README

基于: laracasts/PHP-Vars-To-Js-Transformer

此包允许您轻松地将 Laravel 5 数据输出为 Angular 常量。这对于共享配置设置等非常有用。

基本用法

您将获得一个有用的 Angular 门面,您可以在控制器中使用它。

public function index()
{
    Angular::put([
        'foo' => 'bar',
        'age' => 29
    ]);

    return View::make('footer');
}

使用上面的代码,以下内容将被输出(基于默认配置)。

angular.module('laravelToAngularConstant', []).constant('DATA', {'foo', 'bar', 'age': 29});

现在,您可能希望将该模块添加为您的应用程序的子模块。

angular.module('app', ['laravelToAngularConstant']);

工作原理

它将数据添加到您在配置中定义的 Laravel 视图中(默认为 footer)。通常,您会将该视图文件包含到主布局视图中。

安装

首先通过 Composer 安装此包。

{
    "require": {
        "mikemclin/laravel-to-angular-constant": "~1.0.0"
    }
}

然后设置 Laravel 5 服务提供者

// config/app.php

'providers' => [
    '...',
    'MikeMcLin\Angular\AngularServiceProvider'
];

发布默认配置。

php artisan vendor:publish

这将向 config/angular.php 添加一个新的配置文件。

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | View to Bind Angular Constant To
    |--------------------------------------------------------------------------
    |
    | Set this value to the name of the view (or partial) that
    | you want to prepend your Angular constant to.
    | This can be a single view, or an array of views.
    | Example: 'footer' or ['footer', 'bottom']
    |
    */
    'bind_ng_constant_to_this_view' => 'footer',

    /*
    |--------------------------------------------------------------------------
    | Angular Module
    |--------------------------------------------------------------------------
    |
    | Change this to the Angular module that you want to bind (output)
    | your Angular constant to. You will probably want to include this
    | module as a submodule of your Angular app module.
    |
    */
    'ng_module' => 'laravelToAngularConstant',

    /*
    |--------------------------------------------------------------------------
    | Angular Constant Name
    |--------------------------------------------------------------------------
    |
    | The name of the Angular constant we are attaching the data to.
    |
    */
    'ng_constant' => 'DATA'

];

bind_ng_constant_to_this_view

您需要更新此文件以指定您想要将 Angular 常量添加到哪个视图中。通常,您的页脚是一个不错的选择。

如果您包含类似 layouts/partials/footer 的部分,其中您存储页脚和脚本引用,则使 bind_ng_constant_to_this_view 键等于该路径。幕后,Laravel 将监听该视图的组成,并将 Angular 常量添加到其中。

许可证

MIT 许可证 (MIT)

版权所有 (c) 2015 Mike McLin

特此授予任何获得此软件及其相关文档文件(“软件”)副本的任何人免费权限,不受任何限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权力,并允许向软件提供的人这样做,前提是受以下条件约束

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“现状”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是在合同、侵权或其他行为中产生的,与软件或其使用或其他交易有关。