A means for inspecting and filtering HTTP requests coming into our application is provided by middleware. Creating a new middleware Php artisan make:middleware TestMiddleware Middleware is classified into three categories. Global Middleware Every HTTP request will trigger Global Middleware. Let’s build some middleware. php artisan make:middleware GloMiddleware GloMiddleware is a new middleware that will appear […]
Laravel Controllers
Every controller class extends Laravel’s Base Controller class. Basic Controller We can code all functional elements, as well as communicate with the model in the controller and provide data to the view. Route::get(“/”,[TestController::class,’index’]); TestController class TestController extends Controller{ function index(){ Return view(‘welcome’); }} Single Action Controller If we need to assign a specific controller to […]
How to use route in laravel 8
Except for a URI and closure, a basic Laravel route is empty. Route::get(‘/route’,function(){ Return “Something”; }) Routes for the web interface are defined in web.php And API routes are defined in routes/api.php, which are stateless and allocated to the API middleware group. Available Router Method: There are many router methods available Get Post Put Patch […]
Create and Use Custom Facades in Laravel
Lets create a class called Test class Test{ public function testfunction(){ return ‘testfunctioning’; } public function test2() { return ‘testing2’; } } And bind the class . app()->bind(‘Test’,function (){ return new Test(); }); Create a new TestFacade class. class TestFacade { public static function __callStatic($name,$args){ //app()->make and resolve are same // return app()->make(‘fish’)->$name(); return […]
Laravel Facades
Getting non static function statically is called Facades. Lets set the cache like below in our web route cache()->set(‘name’,”navid”); and get it using dd() dd(cache()->get(‘name’)); Result : “navid” As we can see cache() is not a static method. If we Ctrl+Left Mouse Click on cache(),it will go to that method and we can see the […]
Laravel Service Providers
All services are added to service container using Service Provider. Every service of your application is provided using service provider. Lets open to bootstrap/app.php.We will see below code. $app = new Illuminate\Foundation\Application( $_ENV[‘APP_BASE_PATH’] ?? dirname(__DIR__) ); If we Ctrl+left mouse click on Application, class Application will open. Here Laravel is binding or registering serviceprovider […]
Dependency Injection with Laravel Service Container
Classes may depend on other classes, Laravel addresses this issue with Reflection Classes, and the service container’s goal is to manage class dependencies through dependency injection. Let’s put it to the test with some code. Create a folder called “Billing” and place a class called “PaymentGateWay” inside it. <?php namespace App\Billing; use Illuminate\Support\Str; class PaymentGateWay […]
Request lifecycle of Laravel
We should understand Laravel’s Request lifecycle so that we may interact with/change request/response data or do anything else. First Step Every server, including Apache, that makes a request redirect to the public/ index The index file loads the autoload function and creates an instance of the Laravel application. HTTP/Console kernels Because HTTP/kernels or console kernels […]