Laravel 5扩展核心类


Laravel 5 Extending core class

我正在尝试扩展Laravel 5核心类。我想要实现的是,我可以有自定义url生成器eg。URL::test(),将生成自定义链接。

到目前为止,我已经:

  1. 创建app/Acme/lib文件夹
  2. 添加了作曲器的app/Acme/lib路径。json classmap

    "autoload": {
        "classmap": [
            ....
            app/Acme/lib
         ]
    }
    
  3. 在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');
        }
    }
    
  4. 创建服务提供商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();
        }
    }
    
  5. app/config/app.php中的注册服务提供商

  6. 运行composer dump- auload

现在当我运行{!!URL::测试()! !},我得到404为每个路由

Sorry, the page you are looking for could not be found.
NotFoundHttpException in /vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php line 143:

我错过了什么吗?非常感谢您的帮助

您谈到RouteCollection.php文件中的错误,但您的问题中没有包括它。此外,我会在composer.json中写得不同,像这样:

"autoload": {
"classmap": [
        // ....
        "App''Your_Namespace''" : "app/Acme/lib",
    ]
}