Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending core Laravel 5 functionality

The question is quite simple - is it possible to extend Laravel 5 functionality in any smart way or you need to copy sometimes a lot of code/classes just to achieve what you want.

Because it's quite broad, I'll give you 2 examples.

1) I would like to change urls for edit and create for \Illuminate\Routing\ResourceRegistrar. At the moment the only way I found to achieve that is: extending main Illuminate Application to pass own RoutingServiceProvider, then extend Router, then extend Illuminate\Routing\ResourceRegistrar and finally in bootstrap/app.php use our extended Application class. Quite a lot of modifications just to change 2 lines. Many methods are in fact duplicated just to pass our custom class which code is exact the same except it uses class from other namespace

2) Other similar change - set custom paths for cached config - you need to again extend Application to override method getCachedConfigPath and then make a change in bootstrap/app.php

So the question is - is it the way that some modifications need to be done or maybe the same (for example those 2 above) could be achieved somehow easier not extending core Application class and making so many changes?

like image 938
Marcin Nabiałek Avatar asked Mar 16 '15 18:03

Marcin Nabiałek


1 Answers

You should be able to overwrite the core ResourceRegistrar by binding your own implementation to the service container:

$this->app->bind('\Illuminate\Routing\ResourceRegistrar', function()
{
    return new MyResourceRegistrar();
});

Let MyResourceRegistrar extend the core ResourceRegistrar and you should be able to overwrite the edit and create methods:

class MyResourceRegistrar extends \Illuminate\Routing\ResourceRegistrar
{
    // Overwrite your methods here
}
like image 84
bspellmeyer Avatar answered Sep 16 '22 11:09

bspellmeyer