How to extend laravel 4 core?

I am learning larvel for beginners 4. I want to override specific helper functions. Or add new features in Url, Str, etc. How to do it?

+7
laravel laravel-4
source share
3 answers

Depending on which part of Laravel you want to expand or replace, there are different approaches.

Macros

Adding functions to Str is very simple because of the "macros":

Here is a quick example of adding a function:

Str::macro('test', function($str) { return 'Testing: ' . $str . '!'; }); 

Then you can call this function as expected:

 echo Str::test('text'); // Outputs "Testing: text!" 

Adding functions using macros is supported by Str, Form, HTML and Response .

IOC based extension

To extend the URL, you must use IOC Laravel. This is explained in the docs (as mentioned by BenjaminRH). I agree that this can be a little difficult to understand. Here's a step by step to expand the url:

  • Create a / lib folder to store your extension classes.

  • Add this folder to startup:

    In app / start / global.php add the lib path to the class loader:

     ClassLoader::addDirectories(array( app_path().'/commands', app_path().'/controllers', app_path().'/models', app_path().'/database/seeds', app_path().'/lib' )); 

    Then add the path to the composer.json class layout:

     "autoload": { "classmap": [ "app/commands", "app/controllers", "app/models", "app/database/migrations", "app/database/seeds", "app/tests/TestCase.php", "app/lib" ] }, 
  • Create a custom application UrlGenerator / lib / CustomUrlGenerator.php:

     <?php use \Illuminate\Routing\UrlGenerator; class CustomUrlGenerator extends UrlGenerator { public function test() { return $this->to('/test'); } } 
  • Create the service provider application /lib/CustomRoutingServiceProvider.php:

     <?php use \Illuminate\Routing\RoutingServiceProvider; class CustomRoutingServiceProvider extends RoutingServiceProvider { public function boot() { App::bind('url', function() { return new CustomUrlGenerator( App::make('router')->getRoutes(), App::make('request') ); }); parent::boot(); } } 
  • Register your service provider in /config/app.php:

    Add CustomRoutingServiceProvider to the providers array. For example, immediately after the Workbench provider:

     'Illuminate\Workbench\WorkbenchServiceProvider', 'CustomRoutingServiceProvider', 
  • Run composer dump-autoload from the project root folder.

  • Done. Use as:

     URL::test(); 

NOTE The code is tested, but may contain some errors.

+20
source share

Interestingly, you should mention this. Recently, a section of the entire documentation section has been added that details this. This is very clear and easy to understand. If you use Laravel at all, it may not even surprise you that Laravel actually provides an extend method for a large number of core components.

+1
source share

Following Fnatte's answer, today Laravel versions do some extra processing in the url binding. Rethinking the whole binding is no longer a practical option.

This is how I ended up expanding the url facade.

First create your child class using this template:

 use Illuminate\Routing\UrlGenerator; class YourUrlGenerator extends UrlGenerator { public function __construct(UrlGenerator $url) { parent::__construct($url->routes, $url->request); } // redefine or add new methods here } 

Then add this to the ServiceProvider:

 $url = $this->app['url']; $this->app->singleton('url', function() use ($url) { return new YourUrlGenerator($url); }); 

The point is simply that the initial url binding must be done at least once before we redefine it with our own.

+1
source share

All Articles