5
votes

I'm trying to extend Laravel 5 core class. What i want to achieve is that i can have custom url generators eg. URL::test(), will generate custom link.

So far i have:

  1. Created app/Acme/lib folder
  2. Added app/Acme/lib path to composer.json classmap

    "autoload": {
        "classmap": [
            ....
            app/Acme/lib
         ]
    }
    
  3. 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');
        }
    }
    
  4. 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();
        }
    }
    
  5. Registered service provider in app/config/app.php

  6. Run composer dump-autoload

Now when i run {!! URL::test() !!}, im getting 404 for every route

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

Is there something that i'm missing? Many thanks for any help ..

1

1 Answers

0
votes

You talk about a mistake in the RouteCollection.php file, but you don't include it in your question. Furthermore, I would have written differently in the composer.json, like this:

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