最近PHP 版本升级了7.4,之后用composer下载 Laravel,发现已经更新到8了,然后就试下看着文档操作,看看有啥不一样!
官方文档重点说了Laravel Jetstream,这是一个UI 的脚手架。一般情况下,如果我需要写前端,我会单独分开写的,不会用到Laravel 的脚手架,包括那个资源整合Mix,也不用,所以这个更新其实对我影响不大!忽略
写法上,暂时发现有些地方有变化
路由:
以前写法:
Route::get('/', 'HomeController@index')->name('home');
现在写法:
use App\Http\Controllers\FrontController;
Route::get('/front', [FrontController::class, 'index']);
路由中需要引入 Controller 的路径
Models:
Laravel 8 终于在 app
目录下引入了 Models
子目录来存放模型类文件,所有通过 make:model
命令生成的模型类以后默认都会存放在这个目录下;不过,如果你选择删除这个目录,新生成的模型类将仍然存放到 app
目录下。
app/View:
Laravel8 的app里面有个View 文件夹,里面有个Components的文件夹,上面查了一下,是Blade View Components (组件和插槽),以后有空再写个案例,简单点说,就是将view模块化,有点像Vue。
用户验证
因为懒,所以我经常用 laravel 的自带的登录模块来改,这个和以前版本的有点区别,因为用了jetstream,所以新增了一些写法。
composer require laravel/jetstream
// 使用 Livewire 栈安装 Jetstream...
php artisan jetstream:install livewire
// 使用 Inertia 栈安装 Jetstream...
php artisan jetstream:install inertia
路由就多了一句
Route::middleware(['auth:sanctum', 'verified'])->get(........
在检查一下路由list,就发现多了很多条路由
+--------+----------+----------------------------------+---------------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+----------------------------------+---------------------------------+---------------------------------------------------------------------------------+-----------------------------------------------------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | api/user | | Closure | api |
| | | | | | App\Http\Middleware\Authenticate:sanctum |
| | GET|HEAD | forgot-password | password.request | Laravel\Fortify\Http\Controllers\PasswordResetLinkController@create | web |
| | | | | | App\Http\Middleware\RedirectIfAuthenticated:web |
| | POST | forgot-password | password.email | Laravel\Fortify\Http\Controllers\PasswordResetLinkController@store | web |
| | | | | | App\Http\Middleware\RedirectIfAuthenticated:web |
| | GET|HEAD | front | | App\Http\Controllers\FrontController@index | web |
| | | | | | App\Http\Middleware\Authenticate:sanctum |
| | GET|HEAD | home | home | App\Http\Controllers\HomeController@index | web |
| | | | | | App\Http\Middleware\Authenticate:sanctum |
| | | | | | Illuminate\Auth\Middleware\EnsureEmailIsVerified |
| | GET|HEAD | livewire/livewire.js | | Livewire\Controllers\LivewireJavaScriptAssets@source | |
| | GET|HEAD | livewire/livewire.js.map | | Livewire\Controllers\LivewireJavaScriptAssets@maps | |
| | POST | livewire/message/{name} | livewire.message | Livewire\Controllers\HttpConnectionHandler | web |
| | GET|HEAD | livewire/preview-file/{filename} | livewire.preview-file | Livewire\Controllers\FilePreviewHandler@handle | web |
| | POST | livewire/upload-file | livewire.upload-file | Livewire\Controllers\FileUploadHandler@handle | web |
| | | | | | Illuminate\Routing\Middleware\ThrottleRequests:60,1 |
| | POST | login | | Laravel\Fortify\Http\Controllers\AuthenticatedSessionController@store | web |
| | | | | | App\Http\Middleware\RedirectIfAuthenticated:web |
| | | | | | Illuminate\Routing\Middleware\ThrottleRequests:login |
| | GET|HEAD | login | login | Laravel\Fortify\Http\Controllers\AuthenticatedSessionController@create | web |
| | | | | | App\Http\Middleware\RedirectIfAuthenticated:web |
| | POST | login2 | login2 | App\Http\Controllers\LoginController@authenticate | web |
| | POST | logout | logout | Laravel\Fortify\Http\Controllers\AuthenticatedSessionController@destroy | web |
| | GET|HEAD | profile | | Closure | web |
| | POST | register | | Laravel\Fortify\Http\Controllers\RegisteredUserController@store | web |
| | | | | | App\Http\Middleware\RedirectIfAuthenticated:web |
| | GET|HEAD | register | register | Laravel\Fortify\Http\Controllers\RegisteredUserController@create | web |
| | | | | | App\Http\Middleware\RedirectIfAuthenticated:web |
| | POST | reset-password | password.update | Laravel\Fortify\Http\Controllers\NewPasswordController@store | web |
| | | | | | App\Http\Middleware\RedirectIfAuthenticated:web |
| | GET|HEAD | reset-password/{token} | password.reset | Laravel\Fortify\Http\Controllers\NewPasswordController@create | web |
| | | | | | App\Http\Middleware\RedirectIfAuthenticated:web |
| | GET|HEAD | sanctum/csrf-cookie | | Laravel\Sanctum\Http\Controllers\CsrfCookieController@show | web |
| | POST | two-factor-challenge | | Laravel\Fortify\Http\Controllers\TwoFactorAuthenticatedSessionController@store | web |
| | | | | | App\Http\Middleware\RedirectIfAuthenticated:web |
| | | | | | Illuminate\Routing\Middleware\ThrottleRequests:two-factor |
| | GET|HEAD | two-factor-challenge | two-factor.login | Laravel\Fortify\Http\Controllers\TwoFactorAuthenticatedSessionController@create | web |
| | | | | | App\Http\Middleware\RedirectIfAuthenticated:web |
| | POST | user/confirm-password | | Laravel\Fortify\Http\Controllers\ConfirmablePasswordController@store | web |
| | | | | | App\Http\Middleware\Authenticate:web |
| | GET|HEAD | user/confirm-password | password.confirm | Laravel\Fortify\Http\Controllers\ConfirmablePasswordController@show | web |
| | | | | | App\Http\Middleware\Authenticate:web |
| | GET|HEAD | user/confirmed-password-status | password.confirmation | Laravel\Fortify\Http\Controllers\ConfirmedPasswordStatusController@show | web |
| | | | | | App\Http\Middleware\Authenticate:web |
| | PUT | user/password | user-password.update | Laravel\Fortify\Http\Controllers\PasswordController@update | web |
| | | | | | App\Http\Middleware\Authenticate:web |
| | GET|HEAD | user/profile | profile.show | Laravel\Jetstream\Http\Controllers\Livewire\UserProfileController@show | web |
| | | | | | App\Http\Middleware\Authenticate |
| | | | | | Illuminate\Auth\Middleware\EnsureEmailIsVerified |
| | PUT | user/profile-information | user-profile-information.update | Laravel\Fortify\Http\Controllers\ProfileInformationController@update | web |
| | | | | | App\Http\Middleware\Authenticate:web |
| | POST | user/two-factor-authentication | two-factor.enable | Laravel\Fortify\Http\Controllers\TwoFactorAuthenticationController@store | web |
| | | | | | App\Http\Middleware\Authenticate:web |
| | | | | | Illuminate\Auth\Middleware\RequirePassword |
| | DELETE | user/two-factor-authentication | two-factor.disable | Laravel\Fortify\Http\Controllers\TwoFactorAuthenticationController@destroy | web |
| | | | | | App\Http\Middleware\Authenticate:web |
| | | | | | Illuminate\Auth\Middleware\RequirePassword |
| | GET|HEAD | user/two-factor-qr-code | two-factor.qr-code | Laravel\Fortify\Http\Controllers\TwoFactorQrCodeController@show | web |
| | | | | | App\Http\Middleware\Authenticate:web |
| | | | | | Illuminate\Auth\Middleware\RequirePassword |
| | POST | user/two-factor-recovery-codes | | Laravel\Fortify\Http\Controllers\RecoveryCodeController@store | web |
| | | | | | App\Http\Middleware\Authenticate:web |
| | | | | | Illuminate\Auth\Middleware\RequirePassword |
| | GET|HEAD | user/two-factor-recovery-codes | two-factor.recovery-codes | Laravel\Fortify\Http\Controllers\RecoveryCodeController@index | web |
| | | | | | App\Http\Middleware\Authenticate:web |
| | | | | | Illuminate\Auth\Middleware\RequirePassword
其中可以发现login post 对应的 Controller是 Laravel\Fortify\Http\Controllers\AuthenticatedSessionController@store
打开检查这个方法:
/**
* Attempt to authenticate a new session.
*
* @param \Laravel\Fortify\Http\Requests\LoginRequest $request
* @return mixed
*/
public function store(LoginRequest $request)
{
return $this->loginPipeline($request)->then(function ($request) {
return app(LoginResponse::class);
});
}
/**
* Get the authentication pipeline instance.
*
* @param \Laravel\Fortify\Http\Requests\LoginRequest $request
* @return \Illuminate\Pipeline\Pipeline
*/
protected function loginPipeline(LoginRequest $request)
{
if (Fortify::$authenticateThroughCallback) {
return (new Pipeline(app()))->send($request)->through(array_filter(
call_user_func(Fortify::$authenticateThroughCallback, $request)
));
}
if (is_array(config('fortify.pipelines.login'))) {
return (new Pipeline(app()))->send($request)->through(array_filter(
config('fortify.pipelines.login')
));
}
return (new Pipeline(app()))->send($request)->through(array_filter([
config('fortify.limiters.login') ? null : EnsureLoginIsNotThrottled::class,
Features::enabled(Features::twoFactorAuthentication()) ? RedirectIfTwoFactorAuthenticatable::class : null,
AttemptToAuthenticate::class,
PrepareAuthenticatedSession::class,
]));
}
这个和以前的使用 登录方法已经不同了,以前的登录方法是这样:
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect()->intended('home');
}
return back()->withErrors([
'email' => 'The provided credentials do not match our records.',
]);
为此,我用了以前的登录方法测试下,其实还是可以用的!其他使用基本不变!应该是因为使用了jetstream。
暂时发现这些!真正到项目开发的时候就会发现更多!加油!