IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> Laravel 8 浅谈 -> 正文阅读

[PHP知识库]Laravel 8 浅谈

? ? 最近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。

暂时发现这些!真正到项目开发的时候就会发现更多!加油!

  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-10-22 10:46:41  更:2021-10-22 10:46:58 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 20:47:17-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码