Home > Backend Development > PHP Tutorial > How to Enable CORS in Laravel 5.1?

How to Enable CORS in Laravel 5.1?

DDD
Release: 2024-12-07 17:41:11
Original
745 people have browsed it

How to Enable CORS in Laravel 5.1?

How to Enable CORS in Laravel 5.1

Integrating CORS (Cross-Origin Resource Sharing) into Laravel allows servers to grant permission for cross-origin API calls. This article guides you through specific ways to enable CORS in Laravel version 5.1.

CORS Middleware for Laravel

// app/Http/Middleware/CORS.php

namespace App\Http\Middleware;

use Closure;

class CORS
{

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        header("Access-Control-Allow-Origin: *");

        // ALLOW OPTIONS METHOD
        $headers = [
            'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE',
            'Access-Control-Allow-Headers' => 'Content-Type, X-Auth-Token, Origin'
        ];
        if($request->getMethod() == "OPTIONS") {
            // The client-side application can set only headers allowed in Access-Control-Allow-Headers
            return Response::make('OK', 200, $headers);
        }

        $response = $next($request);
        foreach($headers as $key => $value)
            $response->header($key, $value);
        return $response;
    }

}
Copy after login

Register the CORS Middleware

After creating the middleware, register it in the app/Http/Kernel.php file:

// app/Http/Kernel.php

protected $routeMiddleware = [
        //other middlewares
        'cors' => 'App\Http\Middleware\CORS',
    ];
Copy after login

Apply CORS in Routes

Finally, use the cors middleware in the routes where you want to allow cross-origin API calls:

Route::get('example', array('middleware' => 'cors', 'uses' => 'ExampleController@dummy'));
Copy after login

Note for Laravel ^8.0

In Laravel versions 8.0 and above, it's important to use the following syntax to register the CORS middleware due to namespace changes:

// app/Http/Kernel.php

protected $routeMiddleware = [
        //other middlewares
        'cors' => 'App\Http\Middleware\CORS',
    ];

// routes/web.php or routes/api.php

use App\Http\Controllers\ExampleController;

Route::get('example', [ExampleController::class, 'dummy'])->middleware('cors');
Copy after login

The above is the detailed content of How to Enable CORS in Laravel 5.1?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template