John Davidson

Laravel php - Do something at the beginning and end of function

0 comments
Message:


I'm trying to do something at the beginning and end of each function.


instead of doing so


public function beforeEveryFunctionCore()
{
//something
}

public function afterEveryFunctionCore()
{
//something
}

public function newFunction1(Request $request)
{
$this->beforeEveryFunctionCore();

//function core

$this->afterEveryFunctionCore();

return $something;
}

public function newFunction2(Request $request)
{
$this->beforeEveryFunctionCore();

//function core

$this->afterEveryFunctionCore();

return $something;
}

and so on repeated..(boring)


I would like something like this.


public function beforeEveryFunctionCore()
{
//it does something that is called automatically
}

public function afterEveryFunctionCore()
{
//it does something that is called automatically
}


public function newFunction1(Request $request)
{
//function core

return $something;
}

public function newFunction2(Request $request)
{
//function core

return $something;
}

I know I can do something before every API call with middleware and with construct.
But in this case I talk about any function even those in traits.


How do you think it can be done?



My Answer:
> You can achieve this by using middleware in Laravel. Middleware allows you to run code before and after each request to your application. Here's how you can do it:

1. Create a new middleware by running the following command in your terminal:

php artisan make:middleware BeforeAndAfterMiddleware


2. Open the newly created middleware file located at `app/Http/Middleware/BeforeAndAfterMiddleware.php` and add the following code:
php

namespace App\Http\Middleware;

use Closure;

class BeforeAndAfterMiddleware
{
public function handle($request, Closure $next)
{
// Code to run before the request is handled
// For example:
// do something

$response = $next($request);

// Code to run after the request is handled
// For example:
// do something

return $response;
}
}


3. Register the middleware in your `app/Http/Kernel.php` file. Add the following line to the `$routeMiddleware` array:
php
'beforeAndAfter' => \App\Http\Middleware\BeforeAndAfterMiddleware::class,


4. Now you can apply the middleware to your routes or controllers. You can apply it to all routes by adding the following line to the `$middlewareGroups` array in your `app/Http/Kernel.php` file:
php
'web' => [
// Other middleware...
\App\Http\Middleware\BeforeAndAfterMiddleware::class,
],


5. Alternatively, you can apply the middleware to specific routes or controllers by using the `middleware` method. For example, in your controller:
php
public function newFunction1(Request $request)
{
// Your function code here
}

// Apply the middleware to the function
$this->middleware('beforeAndAfter');


Now, the code in the `BeforeAndAfterMiddleware` will run before and after each request to your application. You can customize the middleware to perform specific actions at the beginning and end of each function.

Rate this post

5 of 5 based on 9084 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us