Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel api enable cors

//I always use an easy method. Just add below lines to publicindex.php file.
//You don't have to use a middleware I think.

header('Access-Control-Allow-Origin: *');  
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
Comment

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

Laravel SPA cors

// add a middleware  (php artisan make:middleware Cors)
// add in your kernel (AppHttpMiddlewareCors::class,)
public function handle(Request $request, Closure $next)
{
  return $next($request)->header('Access-Control-Allow-Origin', '*')
  ->header('Access-Control-Allow-Methods','GET, POST, PUT, PATCH, DELETE, OPTIONS')
  ->header('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type, Accept, Authorization');
}
Comment

PREVIOUS NEXT
Code Example
Php :: laravel rename file if exists 
Php :: Bundling data mvc php 
Php :: Remove values from select list based on condition 
Php :: php artisan make:auth is not working in laravel 8 
Php :: Simple half pyramid pattern 
Php :: woocommerce update_status() email 
Php :: laravel eloquent get current sequence value 
Php :: Allow mass assignment in Laravel 
Php :: Laravel image validation just reloads page and does nothing 
Php :: pass variable in translation larvel 
Php :: Add ACF to single.php 
Php :: how to add to array in single without repetation 
Php :: $var = 1 / 2; in php 
Php :: Condition 
Php :: wordpress not recognizing function during plugin activation 
Php :: Same Taxonomy Add Multiple Post Type 
Php :: df/mpdf/src/Cache.php on line 21 
Php :: somme array php 
Php :: ’ php 
Php :: onde fica o php ini ubuntu 
Php :: cakephp 3 migrations foreign key 
Php :: How to Allow Users to Delete a Post From the Front-End 
Php :: tina4 add debugging 
Php :: small rce php 
Php :: create random username and password php 
Php :: Laravel Http client retry request if fail 
Php :: Registering a variable with $_SESSION. 
Php :: php console lofarray values 
Php :: PHP OOP - Static Methods 
Php :: if request method post php 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =