Sir*_*ant 5 php class extend laravel
我正在尝试扩展Laravel 5核心课程.我想要实现的是我可以拥有自定义URL生成器,例如.URL :: test(),将生成自定义链接.
到目前为止,我有:
添加了composer.json类映射的app/Acme/lib路径
"autoload": {
"classmap": [
....
app/Acme/lib
]
}
Run Code Online (Sandbox Code Playgroud)在Acme/lib/CustomUrlGenerator.php中创建自定义UrlGenerator类
<?php namespace App\Acme\lib;
use \Illuminate\Routing\UrlGenerator;
class CustomUrlGenerator extends UrlGenerator {
public function test() {
return $this->to('/test');
}
}
Run Code Online (Sandbox Code Playgroud)创建服务提供者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();
}
}
Run Code Online (Sandbox Code Playgroud)app/config/app.php中的注册服务提供商
现在当我跑{!! URL :: test()!!},我为每条路线获得404
Sorry, the page you are looking for could not be found.
NotFoundHttpException in /vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php line 143:
Run Code Online (Sandbox Code Playgroud)
有什么东西我不见了吗?非常感谢任何帮助..
您谈论了文件中的错误RouteCollection.php,但没有将其包含在问题中。此外,我会在 中写不同的内容composer.json,如下所示:
"autoload": {
"classmap": [
// ....
"App\\Your_Namespace\\" : "app/Acme/lib",
]
}
Run Code Online (Sandbox Code Playgroud)