我正在尝试扩展Laravel 5核心类。我的目标是能够拥有自定义的 URL 生成器,例如 URL::test() 将会生成自定义链接。
目前为止,我已经做了以下工作:
- Created app/Acme/lib folder
Added app/Acme/lib path to composer.json classmap
"autoload": { "classmap": [ .... app/Acme/lib ] }
Created custom UrlGenerator class in Acme/lib/CustomUrlGenerator.php
<?php namespace App\Acme\lib; use \Illuminate\Routing\UrlGenerator; class CustomUrlGenerator extends UrlGenerator { public function test() { return $this->to('/test'); } }
Created service provider app/Acme/lib/CustomUrlServiceProvider.php
<?php namespace App\Acme\lib; use \Illuminate\Routing\RoutingServiceProvider; class CustomUrlServiceProvider extends RoutingServiceProvider { public function boot() { App::bind('url', function() { return new CustomUrlGenerator( App::make('router')->getRoutes(), App::make('request') ); }); parent::boot(); } }
Registered service provider in app/config/app.php
- Run composer dump-autoload
Sorry, the page you are looking for could not be found.
NotFoundHttpException in /vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php line 143:
我有什么遗漏的吗? 非常感谢任何帮助..