Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel 5.8 cors

Create a simple middleware called Cors:
php artisan make:middleware Cors
  
Add the following code to app/Http/Middleware/Cors.php:

public function handle($request, Closure $next)
{
    return $next($request)
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
        ->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, Authorization, X-Request-With');
}
You can replace the * with localhost or keep it as it is.

Next step is to load the middleware. Add the following line to the $routeMiddleware array in app/Http/Kernel.php.

'cors' => AppHttpMiddlewareCors::class, 
And the final step is to use the middleware on the routes to which you want to set the access origin headers. Assuming you are talking about the new api routes in laravel 5.3, the place to do it is app/Providers/RouteServiceProvider.php, inside the mapApiRoutes() function (you can remove or comment the previous code of the function):

    Route::group([
        'middleware' => ['api', 'cors'],
        'namespace' => $this->namespace,
        'prefix' => 'api',
    ], function ($router) {
         //Add you routes here, for example:
         Route::apiResource('/posts','PostController');
    });
Comment

laravel-cors

composer require fruitcake/laravel-cors
Comment

laravel cors enable

Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"

RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
Comment

what is cors in laravel

Cross-Origin Resource Sharing
sharing resources between different sources
Comment

PREVIOUS NEXT
Code Example
Php :: check if string starts with php 
Php :: compare two arrays and return the difference php 
Php :: angular post phph 
Php :: create function parameters php 
Php :: laravel curl package 
Php :: check if object has method php 
Php :: how to install php dependencies 
Php :: php override trait method and call it 
Php :: laravel session add 
Php :: how to use a session in blade 
Php :: print hello world in php 
Php :: laravel get single column value 
Php :: php array has key 
Php :: Append a text string to WooCommerce single product title 
Php :: how get query logs in laravel 
Php :: transform text to lowercase and replace space with dash php 
Php :: laravel search user details by specific role 
Php :: onclick call route laravel 
Php :: current date in codeigniter 
Php :: date format change in laravel blade 
Php :: update user role wordpress 
Php :: How to Show the Logged in Username in the WordPress 
Php :: wp_enqueue_script 
Php :: laravel add auto increment 
Php :: laravel set config 
Php :: wordpress custom php use wp query 
Php :: get 10 value in array php 
Php :: if condition view page of laravel 
Php :: laravel validation digits 
Php :: phpoffice spreadsheet background color 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =