Trong hướng dẫn này, tôi sẽ cho bạn biết làm thế nào bạn có thể lấy url, url hiện tại với chuỗi truy vấn và phân đoạn url trong Laravel. Laravel có rất nhiều method giúp bạn việc này. Chúng đều là các phương thức Yêu cầu và có thể được gọi tĩnh trong Blade mà không gặp vấn đề gì
Nhận url hoặc đường dẫn hiện tại trong phiến bằng phương thức Yêu cầu Các phương thức chúng tôi có thể sử dụng cho việc này là Yêu cầu. url[], Yêu cầu. fullUrl[], Yêu cầu. đường dẫn [], Yêu cầu. is[] và Yêu cầu. bộ phận[]
đây là ví dụ sau để laravel lấy url hiện tại trong chế độ xem phiến
Nhận đường dẫn hiện tại
ở đây sử dụng Request. phương thức path[] để lấy đường dẫn hiện tại
Path: {{ Request::path[] }}
đầu ra
Path: post/demo
Lấy url hiện tại
ở đây sử dụng Request. phương thức url[]. Nó sẽ trả về toàn bộ URL, nhưng loại bỏ chuỗi truy vấn khỏi nó
Url: {{ Request::url[] }}
đầu ra
Url: //localhost:8000/post/demo
Lấy fullUrl hiện tại
ở đây sử dụng Request. phương thức fullUrl[]. Có thể bạn sẽ không cần đến phương pháp này thường xuyên, nhưng nó có thể hữu ích
FullUrl: {{ Request::fullUrl[] }}
đầu ra
FullUrl: //localhost:8000/post/demo
Nhận URL hiện tại để so sánh mẫu
ở đây sử dụng Request. phương thức is[]. Bạn thậm chí có thể sử dụng ký tự đại diện *. Nó sẽ trả về true nếu tìm thấy kết quả khớp
Mô hình chế độ xem biểu thị dữ liệu cho một chế độ xem hoặc trang cụ thể. Ở dạng đơn giản nhất, một mô hình khung nhìn là một đối tượng PHP đơn giản với một loạt các thuộc tính [đã nhập]
class ProfileViewModel
{
public function __construct[
public User $user,
public array $companies,
public string $action,
] {}
}
Các mô hình xem làm cho các phụ thuộc của một chế độ xem trở nên rõ ràng. Với mô hình chế độ xem, bạn không cần phải đào sâu vào đánh dấu của chế độ xem để tìm ra dữ liệu nào nó yêu cầu
Để sử dụng một mô hình dạng xem, hãy khởi tạo nó trong hành động của bộ điều khiển của bạn và chuyển nó tới dạng xem. Tôi đã xây dựng thói quen chuyển các mô hình dạng xem dưới dạng biến
class ProfileController
{
public function edit[]
{
$viewModel = new ProfileViewModel[
user: Auth::user[],
companies: Companies::all[],
action: action[[ProfileController::class, 'update']],
];
return view['profile.edit', ['view' => $viewModel]];
}
}
2 để giữ cho chúng nhất quán trên tất cả các mẫuclass ProfileController
{
public function edit[]
{
$viewModel = new ProfileViewModel[
user: Auth::user[],
companies: Companies::all[],
action: action[[ProfileController::class, 'update']],
];
return view['profile.edit', ['view' => $viewModel]];
}
}
@foreach[$view->companies as $company]
…
@endforeach
siêu năng lực IDE
Và bây giờ là phần thú vị. nhập gợi ý biến trong khối
class ProfileController
{
public function edit[]
{
$viewModel = new ProfileViewModel[
user: Auth::user[],
companies: Companies::all[],
action: action[[ProfileController::class, 'update']],
];
return view['profile.edit', ['view' => $viewModel]];
}
}
3 ở đầu chế độ xem Blade của bạn và bắt đầu viết HTML@php
/** @var $view App\Http\ViewModels\ProfileViewModel */
@endphp
@foreach[$view->companies as $company]
…
@endforeach
Đây là nơi các mô hình xem tỏa sáng. Một IDE sẽ nhận dạng khai báo
class ProfileController
{
public function edit[]
{
$viewModel = new ProfileViewModel[
user: Auth::user[],
companies: Companies::all[],
action: action[[ProfileController::class, 'update']],
];
return view['profile.edit', ['view' => $viewModel]];
}
}
4 và cung cấp tính năng tự động hoàn thành cho dữ liệu dạng xem. Ngoài ra, nếu bạn đổi tên một trong các thuộc tính của chế độ xem bằng khả năng tái cấu trúc của IDE, thì nó cũng sẽ đổi tên các tập quán trong chế độ xemRefactor đổi tên
class ProfileController
{
public function edit[]
{
$viewModel = new ProfileViewModel[
user: Auth::user[],
companies: Companies::all[],
action: action[[ProfileController::class, 'update']],
];
return view['profile.edit', ['view' => $viewModel]];
}
}
5 trong PhpStorm class ProfileViewModel
{
public function __construct[
public User $user,
- public array $companies,
+ public array $organizations,
public string $action,
] {}
}
class ProfileController
{
public function edit[]
{
$viewModel = new ProfileViewModel[
user: Auth::user[],
companies: Companies::all[],
action: action[[ProfileController::class, 'update']],
];
return view['profile.edit', ['view' => $viewModel]];
}
}
6 và class ProfileController
{
public function edit[]
{
$viewModel = new ProfileViewModel[
user: Auth::user[],
companies: Companies::all[],
action: action[[ProfileController::class, 'update']],
];
return view['profile.edit', ['view' => $viewModel]];
}
}
7 sẽ được cập nhật tự động class ProfileController
{
public function edit[]
{
$viewModel = new ProfileViewModel[
user: Auth::user[],
- companies: Companies::all[],
+ organizations: Companies::all[],
action: action[[ProfileController::class, 'update']],
];
return view['profile.edit', ['view' => $viewModel]];
}
}
- @foreach[$view->companies as $company]
+ @foreach[$view->organizations as $company]
…
@endforeach
Dữ liệu được tính toán
Các mô hình xem cũng là một nơi tuyệt vời để tính toán dữ liệu trước khi gửi nó đến một chế độ xem. Điều này giữ các câu lệnh phức tạp bên ngoài bộ điều khiển và chế độ xem
class ProfileViewModel
{
public bool $isSpatieMember;
public function __construct[
public User $user,
public array $companies,
public array $organizations,
public string $action,
] {
$this->isSpatieMember =
$user->organization->name === 'Spatie';
}
}
Bạn có thể triển khai điều này dưới dạng
class ProfileController
{
public function edit[]
{
$viewModel = new ProfileViewModel[
user: Auth::user[],
companies: Companies::all[],
action: action[[ProfileController::class, 'update']],
];
return view['profile.edit', ['view' => $viewModel]];
}
}
8, nhưng tôi thích xem các mô hình cho những thứ chỉ dùng một lầncông thái học
Bạn có thể chuẩn hóa biến
class ProfileController
{
public function edit[]
{
$viewModel = new ProfileViewModel[
user: Auth::user[],
companies: Companies::all[],
action: action[[ProfileController::class, 'update']],
];
return view['profile.edit', ['view' => $viewModel]];
}
}
2 bằng cách tạo một lớp cơ sở
@foreach[$view->companies as $company]
…
@endforeach
0 thực hiện
@foreach[$view->companies as $company]
…
@endforeach
1use Illuminate\Support\Arrayable;
abstract class ViewModel implements Arrayable
{
public function toArray[]
{
return ['view' => $this];
}
}
- class ProfileViewModel
+ class ProfileViewModel extends ViewModel
{
// …
}
class ProfileController
{
public function edit[]
{
$viewModel = new ProfileViewModel[
user: Auth::user[],
companies: Companies::all[],
action: action[[ProfileController::class, 'update']],
];
return view['profile.edit', ['view' => $viewModel]];
}
}
0độ chi tiết
Ngoài ra, bạn có thể chi tiết hơn và chỉ định dữ liệu chính xác mà bạn muốn hiển thị thay vì chuyển các mô hình
Cách tiếp cận này đòi hỏi nhiều bản mẫu hơn. Tuy nhiên, nó làm cho các phụ thuộc của chế độ xem rõ ràng hơn và có thể tái sử dụng. Ví dụ:
class ProfileController
{
public function edit[]
{
$viewModel = new ProfileViewModel[
user: Auth::user[],
companies: Companies::all[],
action: action[[ProfileController::class, 'update']],
];
return view['profile.edit', ['view' => $viewModel]];
}
}
5 này có thể được sử dụng lại cho các mẫu khác ngoài
@foreach[$view->companies as $company]
…
@endforeach
3