Comparsion Apis in Laravel

In my cases, I want to refactor the codebase but I don鈥檛 want to breaking change any request and response, previously I wrote feature test case in phpunit, but here are several problems: It鈥檚 a mock data for testing flow, it鈥檚 simular but not real. I need to prepare and mock many situations to assert it. I don鈥檛 want to bet and take a risk for refactoring. So I make a new functions: Comparsion, it鈥檚 simple but useful....

April 3, 2022 路 4 min 路 Yish

Define the Version in Laravel

This idea is inspiring from laravel codebase, web.php, it鈥檚 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鈥檚 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