Laravel5.5升级为Laravel5.6的过程与问题

一、修改composer.json文件夹前提php最低7.13

    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=7.1.3",
        "encore/laravel-admin": "1.5.*",
        "estgroupe/laravel-taggable": "5.1.*",
        "fideloper/proxy": "~3.3",
        "laravel/framework": "5.6.*",
        "laravel/tinker": "~1.0",
        "spatie/laravel-feed": "^2.1",
        "stevenyangecho/laravel-u-editor": "~1.4",
        "yuanchao/laravel-5-markdown-editor": "dev-master"
    },
    "require-dev": {
        "filp/whoops": "~2.0",
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "~1.0",
        "phpunit/phpunit": "~7.0",
        "symfony/thanks": "^1.0"
    },

二、清空缓存

php artisan view:clear
php artisan route:clear

三、composer 更新

composer update

四、升级过程中出现报错提示

Undefined class constant 'HEADER_CLIENT_IP'

1、如果你在laravel5.5 升级到laravel5.6后出现报错

打开appHttpMiddlewareTrustProxies.php
Laravel 5.5:

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Fideloper\Proxy\TrustProxies as Middleware;

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array
     */
    protected $proxies;

    /**
     * The current proxy header mappings.
     *
     * @var array
     */
    protected $headers = [
        Request::HEADER_FORWARDED => 'FORWARDED',
        Request::HEADER_X_FORWARDED_FOR => 'X_FORWARDED_FOR',
        Request::HEADER_X_FORWARDED_HOST => 'X_FORWARDED_HOST',
        Request::HEADER_X_FORWARDED_PORT => 'X_FORWARDED_PORT',
        Request::HEADER_X_FORWARDED_PROTO => 'X_FORWARDED_PROTO',
    ];
}

Laravel 5.6:

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Fideloper\Proxy\TrustProxies as Middleware;

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array
     */
    protected $proxies;

    /**
     * The headers that should be used to detect proxies.
     *
     * @var string
     */
    protected $headers = Request::HEADER_X_FORWARDED_ALL;
}

2、打开目录文件

/vendor/fideloper/proxy/config/trustedproxy.php
Laravel 5.5:

return [
    'proxies' => [
        '192.168.1.10',
    ],
    # 'proxies' => '*',
    # 'proxies' => '**',
    'headers' => [
        (defined('Illuminate\Http\Request::HEADER_FORWARDED') ? Illuminate\Http\Request::HEADER_FORWARDED : 'forwarded') => 'FORWARDED',
        Illuminate\Http\Request::HEADER_CLIENT_IP    => 'X_FORWARDED_FOR',
        Illuminate\Http\Request::HEADER_CLIENT_HOST  => 'X_FORWARDED_HOST',
        Illuminate\Http\Request::HEADER_CLIENT_PROTO => 'X_FORWARDED_PROTO',
        Illuminate\Http\Request::HEADER_CLIENT_PORT  => 'X_FORWARDED_PORT',
    ]
];

Laravel 5.6:

return [
    'proxies' => null, // [<ip addresses>,], '*'
     # 'proxies' => ['192.168.1.1'],
     # 'proxies' => '*',
    'headers' => Illuminate\Http\Request::HEADER_X_FORWARDED_ALL,
];

五、升级过程中出现报错提示

array_keys() expects parameter 1 to be array, integer given

1、打开目录文件

/vendor/fideloper/proxy/src/TrustProxies.php
Laravel 5.5:

namespace Fideloper\Proxy;

use Closure;
use Illuminate\Contracts\Config\Repository;

class TrustProxies
{
    protected $config;
    protected $proxies;
    protected $headers;
    public function __construct(Repository $config)
    {
        $this->config = $config;
    }
    public function handle($request, Closure $next)
    {
        $this->setTrustedProxyHeaderNames($request);
        $this->setTrustedProxyIpAddresses($request);
        return $next($request);
    }
    protected function setTrustedProxyIpAddresses($request)
    {
        $trustedIps = $this->proxies ?: $this->config->get('trustedproxy.proxies');

        // We only trust specific IP addresses
        if (is_array($trustedIps)) {
            return $this->setTrustedProxyIpAddressesToSpecificIps($request, $trustedIps);
        }

        // We trust any IP address that calls us, but not proxies further
        // up the forwarding chain.
        // TODO: Determine if this should only trust the first IP address
        //       Currently it trusts the entire chain (array of IPs),
        //       potentially making the "**" convention redundant.
        if ($trustedIps === '*') {
            return $this->setTrustedProxyIpAddressesToTheCallingIp($request);
        }

        // We trust all proxies. Those that call us, and those that are
        // further up the calling chain (e.g., where the X-FORWARDED-FOR
        // header has multiple IP addresses listed);
        if ($trustedIps === '**') {
            return $this->setTrustedProxyIpAddressesToAllIps($request);
        }
    }
    private function setTrustedProxyIpAddressesToSpecificIps($request, $trustedIps)
    {
        $request->setTrustedProxies((array) $trustedIps, $this->getTrustedHeaderSet());
    }
    private function setTrustedProxyIpAddressesToTheCallingIp($request)
    {
        $request->setTrustedProxies($request->getClientIps(), $this->getTrustedHeaderSet());
    }
    private function setTrustedProxyIpAddressesToAllIps($request)
    {
        // 0.0.0.0/0 is the CIDR for all ipv4 addresses
        // 2000:0:0:0:0:0:0:0/3 is the CIDR for all ipv6 addresses currently
        // allocated http://www.iana.org/assignments/ipv6-unicast-address-assignments/ipv6-unicast-address-assignments.xhtml
        $request->setTrustedProxies(['0.0.0.0/0', '2000:0:0:0:0:0:0:0/3'], $this->getTrustedHeaderSet());
    }
    protected function setTrustedProxyHeaderNames($request)
    {
        $trustedHeaderNames = $this->getTrustedHeaderNames();

        if(!is_array($trustedHeaderNames)) { return; } // Leave the defaults

        foreach ($trustedHeaderNames as $headerKey => $headerName) {
            $request->setTrustedHeaderName($headerKey, $headerName);
        }
    }
    protected function getTrustedHeaderNames()
    {
        return $this->headers ?: $this->config->get('trustedproxy.headers');
    }
    protected function getTrustedHeaderSet()
    {
        $trustedHeaderNames = $this->getTrustedHeaderNames();
        $headerKeys = array_keys($this->getTrustedHeaderNames());

        return array_reduce($headerKeys, function ($set, $key) use ($trustedHeaderNames) {
            // PHP 7+ gives a warning if non-numeric value is used
            // resulting in a thrown ErrorException within Laravel
            // This error occurs with Symfony < 3.3, PHP7+
            if(! is_numeric($key)) {
                return $set;
            }

            // If the header value is null, it is a distrusted header,
            // so we will ignore it and move on.
            if (is_null($trustedHeaderNames[$key])) {
                return $set;
            }
            return $set | $key;
        }, 0);
    }
}

Laravel 5.6:

namespace Fideloper\Proxy;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Contracts\Config\Repository;

class TrustProxies
{
    protected $config;
    protected $proxies;
    protected $headers;
    public function __construct(Repository $config)
    {
        $this->config = $config;
    }
    public function handle(Request $request, Closure $next)
    {
        $request::setTrustedProxies([], $this->getTrustedHeaderNames()); // Reset trusted proxies between requests
        $this->setTrustedProxyIpAddresses($request);
        return $next($request);
    }
    protected function setTrustedProxyIpAddresses(Request $request)
    {
        $trustedIps = $this->proxies ?: $this->config->get('trustedproxy.proxies');
        // Only trust specific IP addresses
        if (is_array($trustedIps)) {
            return $this->setTrustedProxyIpAddressesToSpecificIps($request, $trustedIps);
        }
        // Trust any IP address that calls us
        // `**` for backwards compatibility, but is depreciated
        if ($trustedIps === '*' || $trustedIps === '**') {
            return $this->setTrustedProxyIpAddressesToTheCallingIp($request);
        }
    }
    private function setTrustedProxyIpAddressesToSpecificIps(Request $request, $trustedIps)
    {
        $request->setTrustedProxies((array) $trustedIps, $this->getTrustedHeaderNames());
    }
    private function setTrustedProxyIpAddressesToTheCallingIp(Request $request)
    {
        $request->setTrustedProxies([$request->server->get('REMOTE_ADDR')], $this->getTrustedHeaderNames());
    }
    protected function getTrustedHeaderNames()
    {
        return $this->headers ?: $this->config->get('trustedproxy.headers');
    }
}

这样更改之后刷新页面后框架就正常显示了,OK (T | T) !!!

阅读 880

Comments

b

buy cialis 1周前

What i don't understood is in reality how you're no longer actually much more smartly-liked than you might be now. You're so intelligent. You already know thus significantly when it comes to this matter, made me in my opinion imagine it from numerous various angles. Its like men and women don't seem to be involved until it's something to accomplish with Lady gaga! Your personal stuffs great. Always maintain it up!

回复此评论