Hướng dẫn html to text laravel - html để văn bản laravel

Trong dự án, các tài nguyên như ảnh, các file css, javascript, font... được đặt trong thư mục public, để tổ chức tốt hơn chúng ta sẽ tạo ra các thư mục css, js, font chứa các loại file tương ứng. Khi tham chiếu đến các tài nguyên chúng ta có thể sử dụng ngôn ngữ HTML tiêu chuẩn hoặc có một tùy chọn nữa là sử dụng mã giả của gói Laravel HTML, ví dụ:

Hướng dẫn html to text laravel - html để văn bản laravel
{!! HTML::image('images/logo.png', 'All Laravel Logo) !!}

Cài đặt gói Laravel HTML package

Đến thư mục gốc của dự án và sử dụng lệnh composer require để cài đặt gói Laravel HTML (tên chính xác là LaravelCollective). Ở các phiên bản Laravel 4.x gói này được cài đặt sẵn trong Laravel nhưng đến phiên bản Laravel 5.x gói này bị loại bỏ, muốn sử dụng phải cài đặt thêm.

c:\xampp\htdocs\laravel-test>composer require laravelcollective/html
Using version ^5.4 for laravelcollective/html
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing laravelcollective/html (v5.4.1): Downloading (100%)
Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.

Tiếp theo chúng ta sẽ thêm gói này vào các cấu hình của Laravel, mở file config/app.php thêm dòng dưới đây vào phần providers và aliases.

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        ...
        Collective\Html\HtmlServiceProvider::class,
],
'aliases' => [
        ...
        'Form'      => Collective\Html\FormFacade::class,
        'Html'      => Collective\Html\HtmlFacade::class,
],

Ok, phần cài đặt gói LaravelCollective đã được cài đặt ## Chèn ảnh, css, javascript vào blade qua Laravel HTML

Cú pháp để chèn ảnh

Hướng dẫn html to text laravel - html để văn bản laravel
{!! HTML::image('images/logo.png', 'All Laravel Logo') !!}

Cú pháp chèn file css



{!! HTML::style('css/app.min.css') !!}

Cú pháp chèn file javascript



{!! HTML::script('javascript/jquery-1.10.1.min.js') !!}

Xây dựng form nhập liệu bằng Laravel HTML

Chúng ta xem một form dạng HTML

Họ và tên />
Địa chỉ email />
Tiêu đề
Nội dung

Các lệnh liên quan đến mở Form

{!! Form::open(array('url' => 'foo/bar')) !!}
    //
{!! Form::close() !!}

Sử dụng {!! !!} thay cho {{ }} để in ra mã HTML không bị chuyển đổi, xem bài Laravel Blade phần 1 để hiểu hơn cách sử dụng các cách in biến trong Blade. Mặc định khi mở form action sẽ là POST, tuy nhiên chúng ta có thể đẩy dữ liệu lên server theo dạng khác bằng cách thêm tham số (hỗ trợ các phương thức POST, GET, DELETE và PUT):

{!! Form::open(array('url' => 'foo/bar', 'method' => 'put')) !!}

Có rất nhiều các dạng thẻ mở Form khác nhau, mở Form và trỏ đến một route hoặc một phương thức trong Controller

{!! Form::open(array('route' => 'route.name')) !!}

{!! Form::open(array('action' => 'Controller@method')) !!}

Có thể truyền tham số vào cách mở Form này:

c:\xampp\htdocs\laravel-test>composer require laravelcollective/html
Using version ^5.4 for laravelcollective/html
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing laravelcollective/html (v5.4.1): Downloading (100%)
Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.
0

Với các Form có upload file, thêm tham số file:

c:\xampp\htdocs\laravel-test>composer require laravelcollective/html
Using version ^5.4 for laravelcollective/html
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing laravelcollective/html (v5.4.1): Downloading (100%)
Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.
1

CSRF Protect

Laravel cung cấp một phương thức để bảo vệ ứng dụng của bạn khỏi các yêu cầu từ ngoài dạng tấn công cross-site, hệ thống tự động sinh ra ngẫu nhiên một token và đặt trong session, khi bạn sử dụng Form::open với POST, PUT hoặc DELETE thì CSRF token sẽ được tự động thêm vào dạng field ẩn. Nếu bạn sinh mã HTML thuần túy cho trường CSRF, có thể sử dụng phương thức token.

c:\xampp\htdocs\laravel-test>composer require laravelcollective/html
Using version ^5.4 for laravelcollective/html
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing laravelcollective/html (v5.4.1): Downloading (100%)
Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.
2

Các dạng thẻ trong Form

Tạo một nhãn (tương đương thẻ

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        ...
        Collective\Html\HtmlServiceProvider::class,
],
'aliases' => [
        ...
        'Form'      => Collective\Html\FormFacade::class,
        'Html'      => Collective\Html\HtmlFacade::class,
],
4)

c:\xampp\htdocs\laravel-test>composer require laravelcollective/html
Using version ^5.4 for laravelcollective/html
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing laravelcollective/html (v5.4.1): Downloading (100%)
Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.
3

Muốn thêm class vào cho thẻ này truyền thêm tham số:

c:\xampp\htdocs\laravel-test>composer require laravelcollective/html
Using version ^5.4 for laravelcollective/html
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing laravelcollective/html (v5.4.1): Downloading (100%)
Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.
4

Tạo trường nhập liệu text, email, password, upload file

c:\xampp\htdocs\laravel-test>composer require laravelcollective/html
Using version ^5.4 for laravelcollective/html
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing laravelcollective/html (v5.4.1): Downloading (100%)
Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.
5

Tạo checkbox và radio button

c:\xampp\htdocs\laravel-test>composer require laravelcollective/html
Using version ^5.4 for laravelcollective/html
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing laravelcollective/html (v5.4.1): Downloading (100%)
Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.
6

Trường nhập liệu ngày tháng

c:\xampp\htdocs\laravel-test>composer require laravelcollective/html
Using version ^5.4 for laravelcollective/html
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing laravelcollective/html (v5.4.1): Downloading (100%)
Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.
7

Tạo Dropdown list lựa chọn giá trị

c:\xampp\htdocs\laravel-test>composer require laravelcollective/html
Using version ^5.4 for laravelcollective/html
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing laravelcollective/html (v5.4.1): Downloading (100%)
Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.
8

Tạo nút Submit

c:\xampp\htdocs\laravel-test>composer require laravelcollective/html
Using version ^5.4 for laravelcollective/html
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing laravelcollective/html (v5.4.1): Downloading (100%)
Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.
9

Quay lại ví dụ về Form HTML thuần túy ban đầu có thể viết lại theo mã Laravel HTML như sau:

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        ...
        Collective\Html\HtmlServiceProvider::class,
],
'aliases' => [
        ...
        'Form'      => Collective\Html\FormFacade::class,
        'Html'      => Collective\Html\HtmlFacade::class,
],
0

Ok, chúng ta cập nhật code này vào file contact.blade.php trong resources/views/fontend và thử lại đường dẫn http://laravel.dev/contact.

Hướng dẫn html to text laravel - html để văn bản laravel

Tiện đây, chúng ta điều chỉnh lại view contact kế thừa blade template default, kết hợp luôn cùng với style của Bootstrap:

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        ...
        Collective\Html\HtmlServiceProvider::class,
],
'aliases' => [
        ...
        'Form'      => Collective\Html\FormFacade::class,
        'Html'      => Collective\Html\HtmlFacade::class,
],
1

Kết quả khi vào http://laravel.dev/contact trông đẹp hơn hẳn

Hướng dẫn html to text laravel - html để văn bản laravel

Tại sao sử dụng Laravel HTML

Khi sử dụng Laravel HTML code trông dễ nhìn hơn, và truyền các tham số vào các thành phần dễ dàng hơn rất nhiều. Với mở Form chúng ta chỉ cần Form::open là đã có sẵn trường ẩn chứa CSRF token. Với các trường kiểu dropdown select, khi truyền tham số vào nếu sử dụng HTML thuần túy chúng ta phải sử dụng vòng lặp. Ví dụ dưới đây sẽ cho bạn thấy vì sao sử dụng Laravel HTML:

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        ...
        Collective\Html\HtmlServiceProvider::class,
],
'aliases' => [
        ...
        'Form'      => Collective\Html\FormFacade::class,
        'Html'      => Collective\Html\HtmlFacade::class,
],
2

Với thẻ select của HTML như trên chúng ta có thể đơn giản hơn như sau:

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        ...
        Collective\Html\HtmlServiceProvider::class,
],
'aliases' => [
        ...
        'Form'      => Collective\Html\FormFacade::class,
        'Html'      => Collective\Html\HtmlFacade::class,
],
3

Còn rất nhiều các ví dụ khác, bạn hãy từ từ hiểu dần khi tham gia một dự án cụ thể nhé.


CÁC BÀI VIẾT KHÁC