Define the Version in Laravel

This idea is inspiring from laravel codebase, web.php, it’s a useful and simple. // As you can see, it can be used to define the api or code version simplify. if (! defined('DEFAULT_VERSION')) { define('DEFAULT_VERSION', '9.x'); } if (! defined('SHOW_VAPOR')) { define('SHOW_VAPOR', random_int(1, 2) === 1); } // Also you can do something when definition was equaled. if (! defined('SHOW_PROMO')) { $int = random_int(1, 3); if ($int === 1) { define('SHOW_PROMO', 'FORGE'); } elseif ($int === 2) { define('SHOW_PROMO', 'VAPOR'); } elseif ($int === 3) { define('SHOW_PROMO', 'PARTNERS'); } }

April 2, 2022 · 1 min · Yish

Laravel Assert Middleware

In my case, I would like to assert the middleware what I through, Here is my soulution. // TestCase or what you like to put in. public function assertMiddleware($middleware, $request = null, ...$args) { // To be sure it's implemented handle method. if (! method_exists($middleware, 'handle')) { throw new BadMethodCallException(); } // Does it need to customize request? $request = $request?:new Request(); // instanced and called from handle. $instance = App::make($middleware)->handle($request, function($request) { return new JsonResponse($request); }, ....

March 27, 2022 · 1 min · Yish

Artisan Exit Code in Laravel

References from laracasts discussion, also I got a other reference. refs code Useful codes: 1 = failing / failed 2 = not ready 0 = success / no error….

March 25, 2022 · 1 min · Yish

Push middleware into global middleware in laravel

For my situation, I need to append debugbar infomation into APIs response when JSON API respond, here is my solution. You can make other sevice provider for this, but I just want to make a concept for you, so put in AppServiceProvider. What’s position that I want to put on, see in below: class Kernel extends HttpKernel { protected $middleware = [ // Here is prepend position. // \App\Http\Middleware\TrustHosts::class, \App\Http\Middleware\TrustProxies::class, \Illuminate\Http\Middleware\HandleCors::class, \App\Http\Middleware\PreventRequestsDuringMaintenance::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, // Here is push position....

March 25, 2022 · 1 min · Yish

Ignoring specific listeners

We can ignore the listeners when we use event() dispatching event, here is how to implement it. The flow is very simple, passing ignore listeners when we called event, and then ignore it. For example, I have a event with listeners: class EventServiceProvider { protected $listen = [ UserCreated::class => [ SentToAdmin::class, SentToManager::class, NotifyUser::class, // we want to ignore it by specify situation. ] ] } class UserController { public function createByAdmin(Request $request) { $user = User::query()->create($request->validated()); event((new UserCreated)->ignoreListeners([ NotifyUser::class // listener ])); ....

March 10, 2022 · 1 min · Yish

Laravel Customize Query Builder

For my situation, I need to use complex query conditions for my cases, I want to aggregate my conditions in my entity, so I decided to extend the query builder. We know laravel providing the method to allow you customize Eloquent builder: newEloquentBuilder, but I want to more easy to use, almostly, I want to out of the box. So I’m following below to think, starting with my ideal. Model class User extend Model // you can set the contract....

December 4, 2021 · 2 min · Yish