laravel

laravel 5.5升级6.0

直接复制6.0 composer.json替换之前

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.2.5|^8.0",
        "fideloper/proxy": "^4.4",
        "laravel/framework": "^6.20.26",
        "laravel/tinker": "^2.5"
    },
    "require-dev": {
        "facade/ignition": "^1.16.15",
        "fakerphp/faker": "^1.9.1",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^3.0",
        "phpunit/phpunit": "^8.5.8|^9.3.3"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}

执行

composer update

--------------------------------------------

问题:出现报错

In cache.php line 91:
                                         
  Call to undefined function str_slug()  
                                         

Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1

解决方法:因为使用use Illuminate\Support\Str; 替换现在str_slug,

直接找到https://github.com/laravel/laravel/blob/6.x/config/cache.php 替换对应目录下cache.php文件

 

同样问题出现config/session.php,

--------------------------------------------------------------

其它代码对应升级找到版本

Zizaco/entrust已经没有支持laravel6.0以上版本

使用https://github.com/jromero98/entrust/tree/2.0代替,详细可以参考文档

如果

composer require trebol/entrust:0.6

出现错误

使用,忽略php强制要求

composer require trebol/entrust:0.6 --ignore-platform-reqs

 

laravel5/laravel6直接升级laravel8中间出现问题

下面两个文件在更新完后记得对应原框架进行修改

  Problem 1
    - laravel/ui[v4.2.0, ..., 4.x-dev] require illuminate/console ^9.21|^10.0 -> found illuminate/console[v9.21.0, ..., 9.x-dev, v10.0.0, ..., 10.x-dev] but these were not loaded, likely because it conflicts with another require.
    - Root composer.json requires laravel/ui ^4.2 -> satisfiable by laravel/ui[v4.2.0, ..., 4.x-dev].

还有:Trait "Illuminate\Foundation\Auth\AuthenticatesUsers" not found 报错

直接使用这个版本 composer require laravel/ui:^3.4


entrust版本不支持7跟8,直接使用https://github.com/hakobyansen/entrust 替换

https://github.com/hakobyansen/entrust?tab=readme-ov-file

For Laravel 7 run: composer require "codebot/entrust": "^7.0"

For Laravel 8 run: composer require "codebot/entrust": "^8.0"



Auth::routes()已经停止使用了

Auth::routes();

直接下面替换

// Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register');

// Password Reset Routes...
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');

// Email Verification Routes...
Route::get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
Route::get('email/verify/{id}/{hash}', 'Auth\VerificationController@verify')->name('verification.verify');
Route::get('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');
// Authentication Routes...

/laravel/app/Exceptions/Handler.php on line 35

直接找到源码替换:https://raw.githubusercontent.com/laravel/laravel/8.x/app/Exceptions/Handler.php


Class "Fideloper\Proxy\TrustProxies" not found

app/Http/Middleware/TrustProxies.php 找到文件新增/替换:https://raw.githubusercontent.com/laravel/laravel/8.x/app/Http/Middleware/TrustProxies.php


其他问题直接打开

https://github.com/laravel/laravel/blob/8.x/composer.json

对比源码进行修改就可以

 

Target class [Auth\LoginController] does not exist.

您正在使用 Laravel 8。在全新安装的 Laravel 8 中,没有命名空间前缀应用于您的路由加载到的路由组。

“在 Laravel 的早期版本中,RouteServiceProvider包含一个$namespace属性。此属性的值将自动添加到控制器路由定义和对action帮助器/URL::action方法的调用的前缀。在 Laravel 8.x 中,null默认情况下此属性。这意味着没有自动命名空间前缀将由 Laravel 完成。” Laravel 8.x 文档 - 发行说明

如果不使用命名空间前缀,则在路由中引用控制器时,您必须使用控制器的完全限定类名称。

use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']);
// or
Route::get('/users', 'App\Http\Controllers\UserController@index');

如果您更喜欢旧方式:

App\Providers\RouteServiceProvider

public function boot()
{
    ...

    Route::prefix('api')
        ->middleware('api')
        ->namespace('App\Http\Controllers') // <---------
        ->group(base_path('routes/api.php'));

    ...
}

对您想要为其声明命名空间的任何路由组执行此操作。

或者直接

您正在使用 Laravel 8。在全新安装的 Laravel 8 中,没有命名空间前缀应用于您的路由加载到的路由组。

“在 Laravel 的早期版本中,RouteServiceProvider包含一个$namespace属性。此属性的值将自动添加到控制器路由定义和对action帮助器/URL::action方法的调用的前缀。在 Laravel 8.x 中,null默认情况下此属性。这意味着没有自动命名空间前缀将由 Laravel 完成。” Laravel 8.x 文档 - 发行说明

如果不使用命名空间前缀,则在路由中引用控制器时,您必须使用控制器的完全限定类名称。

use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']);
// or
Route::get('/users', 'App\Http\Controllers\UserController@index');

如果您更喜欢旧方式:

App\Providers\RouteServiceProvider

public function boot()
{
    ...

    Route::prefix('api')
        ->middleware('api')
        ->namespace('App\Http\Controllers') // <---------
        ->group(base_path('routes/api.php'));

    ...
}

对您想要为其声明命名空间的任何路由组执行此操作。

或者在 App\Providers\RouteServiceProvider

增加 

    protected $namespace = 'App\Http\Controllers';