简体   繁体   English

在 Google App Engine 柔性环境 PHP (Laravel) 上启用 CORS

[英]Enabling CORS on Google App Engine Flexible environment PHP (Laravel)

0 0

I am facing issues enabling CORS support for a PHP (laravel) application that is hosted using Google App Engine and the flexible environment.我遇到了为使用 Google App Engine 和柔性环境托管的 PHP (laravel) 应用程序启用 CORS 支持的问题。

Every AJAX request using the axios library results in the following error...使用 axios 库的每个 AJAX 请求都会导致以下错误...

Access to XMLHttpRequest at 'https://api.[something].services/request' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.从源“http://localhost:8080”访问“https://api.[something].services/request”的 XMLHttpRequest 已被 CORS 策略阻止:不存在“Access-Control-Allow-Origin”标头在请求的资源上。

You will most likely need to add:您很可能需要添加:

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

The PHP code above indicates that the wildcard character * will allow everybody to make Ajax requests to your web application.上面的 PHP 代码表明通配符*将允许每个人向您的 Web 应用程序发出 Ajax 请求。

header('Access-Control-Allow-Origin: http://example.com');

The PHP code above indicates that example.com has permission to make cross-domain requests to your web application.上面的 PHP 代码表明example.com有权向您的 Web 应用程序发出跨域请求。

Additionally, I recommend that you take a look at this PHP Laravel package to send Cross-Origin Resource Sharing headers with Laravel middleware configuration.此外,我建议您查看这个PHP Laravel 包,以使用 Laravel 中间件配置发送跨源资源共享标头。

Also see:另见:

runtime: php
env: flex
manual_scaling:
  instances: 1
resources:
  cpu: 4
  memory_gb: 4
  disk_size_gb: 20
runtime_config:
  document_root: public

# Ensure we skip ".env", which is only for local development
skip_files:
  - .env
  - .git
  - /vendor/
  - /node_modules/

env_variables:
  # Put production environment variables here.
  APP_LOG: errorlog
  APP_KEY: [REDACTED]
  APP_NAME: MyApp
  APP_ENV: production
  APP_DEBUG: false
  APP_URL: https://example.com
  CUSTOM_CSS: example.css
  FRONTEND_URL: https://web.example.com

  LOG_CHANNEL: stackdriver
  LOG_SLACK_WEBHOOK_URL: https://hooks.slack.com/services/T011E768GNR/B011E7A307P/CGK4zZwTG1tieLwZ1uTx77

  DB_CONNECTION: mysql
  DB_HOST: 127.0.0.1
  DB_PORT: 3306
  DB_DATABASE: dbname
  DB_USERNAME: root
  DB_PASSWORD: [REDACTED]
  DB_SOCKET: "[REDACTED]"

This is a part of app.yml这是 app.yml 的一部分

This is a part of composer.json这是 composer.json 的一部分

protected $middleware = [
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\GaeProxyIp::class,
        \App\Http\Middleware\GaeSetHstsHeader::class,
        \Fruitcake\Cors\HandleCors::class,
    ];

Use Fruitcke\\Cors使用 Fruitcke\\Cors

and cors config im using default和 cors 配置 im 使用默认值

'paths' => ['api/*'],

    /*
    * Matches the request method. `['*']` allows all methods.
    */
    'allowed_methods' => ['*'],

    /*
     * Matches the request origin. `['*']` allows all origins. Wildcards can be used, eg `*.mydomain.com`
     */
    'allowed_origins' => ['*'],

    /*
     * Patterns that can be used with `preg_match` to match the origin.
     */
    'allowed_origins_patterns' => [],

    /*
     * Sets the Access-Control-Allow-Headers response header. `['*']` allows all headers.
     */
    'allowed_headers' => ['*'],

Thanks for your support谢谢您的支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM