×ðÁú¿­Ê±

laravelʵÏÖ¿çÓò»á¼û

ÔÚÏÖ´úµÄwebÓ¦ÓóÌÐòÖÐ £¬¿çÓò×ÊÔ´¹²Ïí£¨cors£©ÒѾ­³ÉΪһÏî»ù±¾¹¦Ð§¡£¶øÔÚʹÓÃlaravel¿ò¼Ü¿ª·¢webÓ¦ÓóÌÐòʱ £¬ÎÒÃÇÒ²¾­³£»áÓöµ½ÐèҪʵÏÖ¿çÓò»á¼ûµÄÇéÐΡ£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃlaravel¿ò¼ÜÀ´ÊµÏÖ¿çÓò×ÊÔ´¹²Ïí £¬ÒÔ±ãÓÚÎÒÃÇ¿ª·¢³öÔ½·¢ÎÞаºÍ¸ßЧµÄwebÓ¦ÓóÌÐò¡£

ʲôÊÇ¿çÓò×ÊÔ´¹²Ïí£¿

ÔÚWeb¿ª·¢ÖÐ £¬¿çÓò×ÊÔ´¹²Ïí£¨CORS£©ÊÇÖ¸ÔÚÒ»¸öÍøÕ¾µÄÒ³ÃæÖÐʹÓÃÁíÒ»¸öÍøÕ¾µÄ×ÊÔ´¡£ÀýÈç £¬Ò»¸öÍøÕ¾£¨AÍøÕ¾£©ÖÐʹÓÃÁíÒ»¸öÍøÕ¾£¨BÍøÕ¾£©µÄAPI½Ó¿Ú £¬»ñÈ¡Êý¾ÝµÈµÈ¡£ÓÉÓÚä¯ÀÀÆ÷µÄͬԴսÂÔ£¨Same-Origin Policy£© £¬Ö±½ÓʹÓÃÁíÒ»¸öÍøÕ¾µÄ×ÊÔ´ÊDz»±»ÔÊÐíµÄ¡£ÔÚÕâÖÖÇéÐÎÏ £¬ÎÒÃÇÐèҪʹÓÿçÓò×ÊÔ´¹²ÏíÀ´Èòî±ðÓòÖ®¼äµÄÊý¾Ý¾ÙÐн»»¥¡£

LaravelÔõÑùʵÏÖ¿çÓò»á¼û£¿

Laravel¿ò¼ÜÌṩÁËÐí¶àÊÊÓõÄÖÐÐļþ £¬ËüÃÇ¿ÉÒÔÇáËɵØʵÏÖ¿çÓò»á¼û¡£ÏÂÃæÎÒÃǽ«ÏÈÈÝÁ½ÖÖʵÏÖ·½·¨¡£

µÚÒ»ÖÖʵÏÖ·½·¨£ºÊ¹ÓÃLaravelµÄCORSÖÐÐļþ

Ê×ÏÈÐèÒªÒýÈëÒ»¸öµÚÈý·½CORSÖÐÐļþ°ü £¬ÎÒÃÇ¿ÉÒÔʹÓÃlaravel-corsÕâ¸ö°ü £¬Ïêϸ°ì·¨ÈçÏ£º

×°ÖÃlaravel-cors°ü

composer require barryvdh/laravel-cors

µÇ¼ºó¸´ÖÆ

×¢²áÖÐÐļþ

ÔÚappHttpKernel.phpÎļþÖÐ ×¢²áÖÐÐļþ£º

protected $middleware = [
    // Other middlewares
    BarryvdhCorsHandleCors::class,
];

µÇ¼ºó¸´ÖÆ

ÉèÖÃCORS²ÎÊý

½Ó×ÅÔÚconfig/cors.phpÎļþÖÐÉèÖÿçÓò²ÎÊý £¬ÈçÏÂËùʾ£º

<?php
return [
    'paths' => [
        'api/*',
        '/*'
    ],

    'allowed_methods' => [
        'GET',
        'POST',
        'PUT',
        'DELETE',
        'OPTIONS',
    ],

    'allowed_origins' => [
        '*',
    ],

    'allowed_origins_patterns' => [],

    'allowed_headers' => [
        'Content-Type',
        'X-Requested-With',
        'Authorization',
        'Accept',
        'Origin',
    ],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => true,
];

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëÖÐ £¬pathsÊôÐÔ½ç˵ÁËÐèÒªÖ§³Ö¿çÓòµÄAPI·¾¶ £¬allowed_methodsÊôÐÔ½ç˵ÁËÔÊÐí¿çÓòµÄHTTPÒªÁì £¬allowed_originsÊôÐÔ½ç˵ÁËÔÊÐí¿çÓòµÄȪԴ£¨*ÌåÏÖËùÓÐȪԴ£© £¬allowed_headersÊôÐÔ½ç˵ÁËÔÊÐí¿çÓò´«ÊäµÄÇëÇóÍ·¡£

µÚ¶þÖÖʵÏÖ·½·¨£ºÊ¹ÓÃLaravelµÄmiddleware

ͨ¹ý×Ô½ç˵LaravelÖÐÐļþÀ´ÊµÏÖ¿çÓò»á¼ûÒ²ÊÇÒ»ÖÖ²»´íµÄÑ¡Ôñ¡£ÔÚÕâÖÖ·½·¨Ï £¬ÎÒÃÇÐèÒª×Ô¼º±àдһ¸öLaravelÖÐÐļþÀ´´¦Öóͷ£¿çÓò»á¼ûÇëÇó¡£ÏÂÃæÊÇÒ»¸ö»ù±¾µÄʵÏÖʾÀý£º

½¨Éè¿çÓòÖÐÐļþ

Ê×ÏÈ £¬ÎÒÃÇÐèÒªÔÚapp/Http/MiddlewareĿ¼Ï½¨ÉèÒ»¸öCorsMiddleware.phpÎļþ £¬ÎļþÄÚÈÝÈçÏ£º

<?php
namespace AppHttpMiddleware;

use Closure;

class CorsMiddleware
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        $response->header('Access-Control-Allow-Origin', '*')
                 ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
                 ->header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Authorization');

        return $response;
    }
}

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëÖеÄhandleÒªÁì¿ÉÒÔÔÚÿ¸öÇëÇóµ½À´Ê±Ö´ÐÐ £¬Í¨¹ýÉèÖÃÏìӦͷ²¿µÄ·½·¨À´ÔÊÐí¿çÓò»á¼û¡£

×¢²á¿çÓòÖÐÐļþ

½Ó×ÅÎÒÃÇÐèÒª½«CorsMiddleware×¢²áµ½Laravel¿ò¼ÜÖÐ £¬ÒÔ±ãÓÚʹÓá£ÔÚapp/Http/Kernel.phpÎļþÖÐÌí¼ÓÈçÏ´úÂ룺

protected $routeMiddleware = [
    // Other middleware
    'cors' => AppHttpMiddlewareCorsMiddleware::class,
];

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëÖеÄCorsMiddleware::class¾ÍÊÇÎÒÃÇ×Ô½ç˵µÄCorsMiddlewareÖÐÐļþ £¬ÔÚÕâÀï×¢²áºó¾Í¿ÉÒÔÔÚ³ÌÐòÖÐʹÓÃÁË¡£

ʹÓÿçÓòÖÐÐļþ

ÔÚÐèÒªÖ§³Ö¿çÓòµÄ¿ØÖÆÆ÷»ò·ÓÉÖÐ £¬Ê¹ÓÃÒѾ­×¢²áºÃµÄCorsMiddleware £¬ÀýÈ磺

Route::group(['middleware' => ['cors']], function () {
    // ÕâÀï¿ÉÒÔÌí¼Ó¿çÓòAPI
});

µÇ¼ºó¸´ÖÆ

µ½ÕâÀï £¬ÎÒÃÇÒѾ­ÀֳɵØͨ¹ýÁ½ÖÖ·½·¨À´ÊµÏÖÁËLaravel¿ò¼ÜϵĿçÓò»á¼û¡£ÔÚÏÖʵ¿ª·¢ÖÐ £¬ÎÒÃÇ¿ÉÒÔƾ֤ÏêϸÐèÇóÑ¡ÔñÊʺÏ×Ô¼ºµÄ·½·¨À´ÊµÏÖ¿çÓò»á¼û £¬ÈÃ×ðÁú¿­Ê±WebÓ¦ÓóÌÐòÔ½·¢ÎÞаºÍ¸ßЧ¡£

ÒÔÉϾÍÊÇlaravelʵÏÖ¿çÓò»á¼ûµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿