欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Laravel自定義視圖組件的示例-創(chuàng)新互聯

這篇文章主要介紹Laravel 自定義視圖組件的示例,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

江口ssl適用于網站、小程序/APP、API接口等需要進行數據傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

Laravel 的視圖合成器可將數據與指定視圖綁定在一起,避免了重復編寫代碼。

View::composer('profile', 'App\Http\View\Composers\ProfileComposer');

由于數據的生成和渲染是分開進行的,理解起來不夠直觀。因此,可以采用視圖組件的方式將兩者進行封裝。

<?php
namespace App\ViewComponents;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
class ExampleComponent implements Htmlable
{
    private $color;
    private $request;
    public function __construct(Request $request, string $color)
    {
        $this->color = $color;
        $this->request = $request;
    }
    public function toHtml()
    {   
        return View::make('example')
            ->with('color', $this->color)
            ->render();
    }
}

在視圖中使用

{{ app()->makeWith(App\ViewComponents\ExampleComponent::class,['color' => 'green'])->toHtml() }}

封裝指令

Blade::directive('render', function ($expression) {
    list($class, $params) = explode(',', $expression, 2);
    $class = "App\\ViewComponents\\".trim($class, '\'" ');
    return "<?php echo app()->makeWith('$class', $params)->toHtml(); ?>";
});

使用指令

@render('ExampleComponent', ['color' => 'green'])

參考資料

spatie/laravel-view-components: A better way to connect data with view rendering in Laravel
Introducing View Components in Laravel, an alternative to View Composers - Laravel News

以上是“Laravel 自定義視圖組件的示例”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯行業(yè)資訊頻道!

當前名稱:Laravel自定義視圖組件的示例-創(chuàng)新互聯
本文網址:http://chinadenli.net/article14/ccjpde.html

成都網站建設公司_創(chuàng)新互聯,為您提供定制開發(fā)關鍵詞優(yōu)化外貿建站網站內鏈自適應網站響應式網站

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

成都app開發(fā)公司