
Bài 9: Tìm hiểu về View trong Laravel
Trong mô hình MVC, chữ cái V là viết tắt của View. Nó phân tách application logic và presentation logic. View được lưu trữ trong thư mục resources/views.
1. Tạo view trong Laravel
Để tạo view trong Laravel các bạn một file mới trong thư mục resources/views:
view-name.blade.php
2. Ví dụ
a. Tạo View
Chúng ta tạo file view home.blade.php với nội dung như sau:
<html>
<body>
<h1>Hello, World</h1>
</body>
</html>
b. Gọi View từ Route
Tạo tiếp Route gọi đến View home:
Route::get('/home', function() {
return view('home');
});
Tiếp theo chúng ta truy cập vào link /home: http://example.com/home kết quả trả về sẽ là dòng chữ Hello, World được hiển thị.
c. Gọi View từ Controller
Ngoài cách gọi View từ Route chúng ta có thể gọi View từ Controller và thường là sẽ sử dụng cách này. Chúng ta tạo HomeController
php artisan make:controller HomeController
Nội dung file HomeController sẽ như sau:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function index()
{
return view('home');
}
}
Khi đó Route chúng ta sẽ sửa thành như sau:
Route::get('/home', '[email protected]');
Khi chúng ta truy cập http://example.com/home thì kết quả trả về vẫn sẽ là dòng chữ Hello, World được hiển thị.
3. Kiểm tra sự tồn tại của View
Nếu bạn cần xác định xem View có tồn tại hay không trước khi gọi đến nó, bạn có thể dùng View facade. Phương pháp exists, sẽ trả lại true nếu view tồn tại:
if (\View::exists('home')) {
//
}
Như ví dụ ở trên chúng ta có thể sửa lại Controller như sau:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function index()
{
if (\View::exists('home')) {
return view('home');
}
return abort('404');
}
}
Như vậy nếu file View home.blade.php tồn tại thì sẽ trả về View home còn nếu file không tồn tại thì sẽ trả về lỗi 404.
4. Truyền dữ liệu sang View
Chúng ta có thể truyền dữ liệu sang View dưới dạng mảng hoặc sử dụng phương thức with()
a. Truyền dưới dạng mảng
return view('home', ['name' => 'VietLaravel']);
b. Sử dụng with()
return view('home')->with('name', 'VietLaravel');
c. Ví dụ
Trong Controller chúng ta viết như sau:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function index()
{
if (View::exists('home')) {
return view('home',['name' => 'VietLaravel']);
// hoặc
// return view('home')->with('name', 'VietLaravel');
}
return abort('404');
}
}
Lúc này trong View chúng ta sẽ gọi đến biến $name
<html>
<body>
<h1>Hello: {{$name}}</h1>
</body>
</html>
Kết quả hiển thị sẽ là: Hello VietLaravel
5. Chia sẻ dữ liệu cho nhiều View
Đôi khi, bạn có thể cần chia sẻ một phần dữ liệu với tất cả các Views trong project của bạn. Bạn hoàn toàn có thể làm điều này với phương thức share. Thông thường, bạn nên làm điều này bằng cacsg sử dụng phương thức boot trong service provider's . Bạn có thể tự do thêm chúng vào AppServiceProvider hoặc tạo service provider riêng:
Để tạo Provider chúng ta dùng lệnh sau:
php artisan make:provider provider-name
Sau khi tạo provider sẽ nằm trong thư mục: App/Providers . Ví dụ chúng ta tạo SidebarProvider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class SidebarProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
Ví dụ chúng ta có một sidebar Random Post và muốn share tới tất cả các View
Chúng ta sẽ sửa lại file SidebarProvider như sau
<?php
namespace App\Providers;
use App\Post;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class SidebarServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
$randomPost = Post::select('title', 'slug', 'image', 'created_at')
->inRandomOrder()->where('status', '=', 'PUBLISHED')
->take(5)->get()->toArray();
View::share('randomPost', $randomPost);
}
}
Như vậy trong tất cả các View chúng ta chỉ cần gọi tới biến $randomPost và tiến hành sử dụng như bình thường.
Ngoài ra các bạn có thể tham khảo thêm tại đây: https://laravel.com/docs/5.8/views
Các bạn copy bài viết sang website khác, vui lòng ghi nguồn VietLaravel.com. Xin cảm ơn.
Post Comment