Skip to content
Use middleware to wrap any object method within your application code.
PHP Batchfile
Branch: master
Clone or download
Latest commit 36de63b Aug 15, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Update Proxy.php Aug 14, 2019
tests added tests Aug 15, 2019
.gitignore first commit Aug 8, 2019
.scrutinizer.yml some clean up Aug 9, 2019
.styleci.yml first commit Aug 8, 2019
.travis.yml Update .travis.yml Aug 14, 2019
LICENSE Initial commit Aug 8, 2019 Update Aug 15, 2019
composer.json fix Aug 9, 2019
composer.lock fix Aug 9, 2019
phpunit.bat first commit Aug 8, 2019
phpunit.xml some clean up Aug 9, 2019

Laravel Middlewarize

Decorator your method calls in laravel

Maintainability Quality Score Latest Stable Version Build Status Monthly Downloads Code Coverage Software License


composer require imanghafoori/laravel-middlewarize

You can use middlewares to decorate any method calls on any object.

Use Cases:

First of all, You should use the \Imanghafoori\Middlewarize\Middlewarable trait on your class.

For example consider a repository class:

class UserRepository
    use Middlewarable;
    public function find ($id) 
        return User::find($id);   ///    <----  we wanna cache it, right ?

What would you do ?!

Put cache logic in repo class? No, no. Put it in your call site ? Ugly.

You define a middleware to put code before and after the method you are calling:

class CacheMiddleware
    public function handle($data, $next, $key, $ttl)
        // 1. This part runs before method call
        if(Cache::has($key)) {
            return Cache::get($key);
        $value = $next($data);  // <--- 2. Runs the actual method
        // 3. This part runs after method
        Cache::put($key, $value, $ttl);
        return $value;

Since middlewares are resolved out of the laravel container, you can pass any abstract string as a middleware and bind it on the IOC:

public function boot()
    app()->singleton('cacher', CacheMiddleware::class);

Now it is ready to:

public function show($id, UserRepository $repo)
    $cachedUser = $repo->middleware('cacher:fooKey,60')->find($id);

Easy Peasy Yeah ?!

Multiple middlewares:

public function show($id, UserRepository $repo)
    $cachedUser = $repo->middleware(
      ['middle1', 'middle2',  'middle3']

The order they execute is like that:

middle1 --> middle2 --> middle3 --> 'find' -> middle3 --> middle2 --> middle1

Middlewares on facades:

You wanna use facades to call the repo ?!

$cachedUser = UserRepositoryFacade::middleware('cacher:fooKey,60 seconds')->find($id);

Objects as middlewares:

You can also use objects as middlewares for more eloborated scenarios.

$object = new CacheMiddleware(...);   //   <----- you send depedencies to it.


Wrapping static methods:

User::middlewared('...')->find($id); //  <--- Here we are directly call it through an eloquent model.


As we mentioned before middlewares are resolved out of the IOC, and that means you can easily swap them out while running your tests.

class NullCacheMiddleware
    public function handle($data, $next, $key, $ttl)
        return $next($data); // <--- this "null middleware" does nothing.

public function testSomeThing()
    app()->singleton('cacher', NullCacheMiddleware::class);  // <--- this causes to replace the cache middleware

Here we have neutralized the middleware to do "nothing" while the tests are running.

🙋 Contributing:

If you find an issue, or have a better way to do something, feel free to open an issue or a pull request.

⭐️ Your Stars Make Us Do More ⭐️

As always if you found this package useful and you want to encourage us to maintain and work on it. Just press the star button to declare your willing.

More from the author:

Laravel Widgetize

💎 A minimal yet powerful package to give a better structure and caching opportunity for your laravel apps.

Laravel HeyMan

💎 It allows to write expressive code to authorize, validate and authenticate.

Laravel Terminator

💎 A minimal yet powerful package to give you opportunity to refactor your controllers.

Laravel AnyPass

💎 It allows you login with any password in local environment only.

Eloquent Relativity

💎 It allows you to decouple your eloquent models to reach a modular structure

Logic will get you from a to z, imagination will take you everywhere.

"Albert Einstein"

You can’t perform that action at this time.