casa-parks/extract-routes
Composer 安装命令:
composer require casa-parks/extract-routes
包简介
Casa-Parks' Laravel route extracter.
README 文档
README
Introduction
Extract Routes is a simple provision of route listing, designed for providing use to the front end (IE: in JavaScript).
License
Extract Routes is open-sourced software licensed under the MIT license
Installation
To get started with Extract Routes, use Composer to add the package to your project's dependencies:
composer require casa-parks/extract-routes
Configuration
After installing, register the CasaParks\ExtractRoutes\ExtractRoutesServiceProvider in your config/app.php configuration file:
'providers' => [ // Other service providers... CasaParks\ExtractRoutes\ExtractRoutesServiceProvider::class, ],
Basic Usage
Create a simple view composer, like so:
<?php namespace App\Composers; use CasaParks\ExtractRoutes\Service as RoutesExtractor; use Illuminate\Contracts\View\View; class RoutesComposer { /** * The routes extractor. * * @var \CasaParks\ExtractRoutes\Service */ protected $extractor; /** * Whether the data is cached or not. * * @var bool */ protected $cached; /** * The view data. * * @var array */ protected $data; /** * Creates a new routes composer. * * @param \CasaParks\ExtractRoutes\Service $extractor */ public function __construct(RoutesExtractor $extractor) { $this->extractor = $extractor; } /** * Compose the view. * * @param \Illuminate\Contracts\View\View $view * * @return void */ public function compose(View $view) { if (! $this->cached) { $this->cache(); } $view->with($this->data); } /** * Cache the data. * * @return void */ protected function cache() { $this->cached = true; // We don't want to include any admin / api routes. $routes = $this->extractor->filterOnly('middleware', 'guest', 'auth'); $this->data = compact('routes'); } }
Add this view composer, into your app (or composer) service provider's boot method:
/** * Register any composers for your application. * * @return void */ public function boot() { // ... // assuming `layout` is your common layout template. $this->app['view']->composer('layout', 'App\Composers\RoutesComposer'); // ... }
In your common layout template file:
<!-- ... --> <head> <!-- ... --> <script>window.routes = {!! $routes->toJson() !!}</script> <!-- ... --> </head> <!-- ... -->
Then utilise as required in your JavaScript.
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-03-15