Cách gọi hàm điều khiển laravel trong javascript?

Thay vì xác định tất cả logic xử lý yêu cầu của bạn dưới dạng Đóng trong các tệp định tuyến, bạn có thể muốn tổ chức hành vi này bằng cách sử dụng các lớp Trình điều khiển. Bộ điều khiển có thể nhóm logic xử lý yêu cầu liên quan thành một lớp duy nhất. Bộ điều khiển được lưu trữ trong thư mục

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

6

Bộ điều khiển cơ bản

Xác định bộ điều khiển

Dưới đây là một ví dụ về lớp điều khiển cơ bản. Lưu ý rằng bộ điều khiển mở rộng lớp bộ điều khiển cơ sở có trong Laravel. Lớp cơ sở cung cấp một số phương thức tiện lợi như phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

7, có thể được sử dụng để đính kèm phần mềm trung gian vào các hành động của bộ điều khiển

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

Bạn có thể xác định tuyến đường đến hành động của bộ điều khiển này như vậy

Bây giờ, khi một yêu cầu khớp với URI tuyến đường đã chỉ định, phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

8 trên lớp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

9 sẽ được thực thi. Tất nhiên, các tham số route cũng sẽ được truyền cho phương thức

{tip} Bộ điều khiển không bắt buộc phải mở rộng lớp cơ sở. Tuy nhiên, bạn sẽ không có quyền truy cập vào các tính năng tiện lợi như phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

7,

Route::get['user/{id}', 'ShowProfile'];

1 và

Route::get['user/{id}', 'ShowProfile'];

2

Bộ điều khiển & Không gian tên

Điều rất quan trọng cần lưu ý là chúng ta không cần chỉ định không gian tên bộ điều khiển đầy đủ khi xác định tuyến bộ điều khiển. Vì

Route::get['user/{id}', 'ShowProfile'];

3 tải các tệp tuyến đường của bạn trong một nhóm tuyến đường có chứa không gian tên, nên chúng tôi chỉ chỉ định phần tên lớp xuất hiện sau phần

Route::get['user/{id}', 'ShowProfile'];

4 của không gian tên

Nếu bạn chọn lồng các bộ điều khiển của mình sâu hơn vào thư mục

Route::get['user/{id}', 'ShowProfile'];

4, chỉ cần sử dụng tên lớp cụ thể liên quan đến không gian tên gốc của

Route::get['user/{id}', 'ShowProfile'];

4. Vì vậy, nếu lớp bộ điều khiển đầy đủ của bạn là

Route::get['user/{id}', 'ShowProfile'];

7, bạn nên đăng ký các tuyến đến bộ điều khiển như vậy

Bộ điều khiển hành động đơn lẻ

Nếu bạn muốn định nghĩa một bộ điều khiển chỉ xử lý một hành động, bạn có thể đặt một phương thức

Route::get['user/{id}', 'ShowProfile'];

8 trên bộ điều khiển

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

Khi đăng ký định tuyến cho bộ điều khiển hành động đơn lẻ, bạn không cần chỉ định phương thức

Route::get['user/{id}', 'ShowProfile'];

Phần mềm trung gian điều khiển

Phần mềm trung gian có thể được gán cho các tuyến của bộ điều khiển trong các tệp tuyến của bạn

Tuy nhiên, sẽ thuận tiện hơn khi chỉ định phần mềm trung gian trong hàm tạo của bộ điều khiển của bạn. Sử dụng phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

7 từ hàm tạo của bộ điều khiển, bạn có thể dễ dàng gán phần mềm trung gian cho hành động của bộ điều khiển. Bạn thậm chí có thể hạn chế phần mềm trung gian chỉ với một số phương thức nhất định trên lớp trình điều khiển

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

7

Bộ điều khiển cũng cho phép bạn đăng ký phần mềm trung gian bằng cách sử dụng Đóng. Điều này cung cấp một cách thuận tiện để xác định phần mềm trung gian cho một bộ điều khiển mà không cần xác định toàn bộ lớp phần mềm trung gian

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

8

{tip} Bạn có thể chỉ định phần mềm trung gian cho một tập hợp con các hành động của bộ điều khiển; . Thay vào đó, hãy cân nhắc chia bộ điều khiển của bạn thành nhiều bộ điều khiển nhỏ hơn

Bộ điều khiển tài nguyên

Định tuyến tài nguyên Laravel gán các tuyến "CRUD" điển hình cho bộ điều khiển với một dòng mã. Ví dụ: bạn có thể muốn tạo bộ điều khiển xử lý tất cả các yêu cầu HTTP cho "ảnh" được ứng dụng của bạn lưu trữ. Sử dụng lệnh Artisan

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

70, chúng ta có thể nhanh chóng tạo một bộ điều khiển như vậy

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

0

Lệnh này sẽ tạo bộ điều khiển tại

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

71. Bộ điều khiển sẽ chứa một phương thức cho từng hoạt động tài nguyên có sẵn

Tiếp theo, bạn có thể đăng ký một tuyến đường tháo vát tới bộ điều khiển

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

2

Khai báo tuyến đơn này tạo nhiều tuyến để xử lý nhiều hành động khác nhau trên tài nguyên. Bộ điều khiển được tạo sẽ có các phương thức được lập sẵn cho từng hành động này, bao gồm các ghi chú thông báo cho bạn về các động từ HTTP và URI mà chúng xử lý

Các hành động được xử lý bởi bộ điều khiển tài nguyên

Động từURIAhành độngTên tuyến đườngGET____172indexphotos. chỉ mụcGET____173tạo ảnh. tạoPOST____172storephotos. cửa hàngGET____175showphotos. hiển thịGET

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

76chỉnh sửa ảnh. chỉnh sửaPUT/PATCH

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

75cập nhật ảnh. cập nhậtXÓA

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

75hủy ảnh. hủy hoại

Chỉ định Mô hình Tài nguyên

Nếu bạn đang sử dụng liên kết mô hình định tuyến và muốn các phương thức của bộ điều khiển tài nguyên gõ gợi ý một thể hiện mô hình, bạn có thể sử dụng tùy chọn

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

79 khi tạo bộ điều khiển

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

1

Phương pháp biểu mẫu giả mạo

Vì các biểu mẫu HTML không thể thực hiện các yêu cầu

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

80,

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

81 hoặc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

82, nên bạn sẽ cần thêm một trường

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

83 ẩn để giả mạo các động từ HTTP này. Người trợ giúp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

84 có thể tạo trường này cho bạn

Định tuyến tài nguyên một phần

Khi khai báo một tuyến tài nguyên, bạn có thể chỉ định một tập hợp con các hành động mà bộ điều khiển sẽ xử lý thay vì toàn bộ các hành động mặc định

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

7

Đặt tên các tuyến tài nguyên

Theo mặc định, tất cả các hành động của bộ điều khiển tài nguyên đều có tên tuyến đường;

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

9

Đặt tên tham số tuyến đường tài nguyên

Theo mặc định,

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

86 sẽ tạo các tham số định tuyến cho các tuyến tài nguyên của bạn dựa trên phiên bản "số ít" của tên tài nguyên. Bạn có thể dễ dàng ghi đè điều này trên cơ sở từng tài nguyên bằng cách chuyển

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

87 vào mảng tùy chọn. Mảng

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

87 phải là một mảng kết hợp của tên tài nguyên và tên tham số

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

0

Ví dụ trên tạo các URI sau cho tuyến đường

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

8 của tài nguyên

Bản địa hóa URI tài nguyên

Theo mặc định,

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

86 sẽ tạo các URI tài nguyên bằng các động từ tiếng Anh. Nếu bạn cần bản địa hóa các động từ hành động

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

01 và

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

02, bạn có thể sử dụng phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

03. Điều này có thể được thực hiện theo phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

04 của

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

05 của bạn

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

1

Khi các động từ đã được tùy chỉnh, đăng ký tuyến tài nguyên, chẳng hạn như

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

06 sẽ tạo ra các URI sau

Bổ sung bộ điều khiển tài nguyên

Nếu bạn cần thêm các tuyến bổ sung vào bộ điều khiển tài nguyên ngoài tập hợp các tuyến tài nguyên mặc định, bạn nên xác định các tuyến đó trước lệnh gọi tới

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

86;

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

2

{tip} Hãy nhớ tập trung vào bộ điều khiển của bạn. Nếu bạn thấy mình thường xuyên cần các phương thức bên ngoài tập hợp các hành động tài nguyên thông thường, hãy xem xét việc chia bộ điều khiển của bạn thành hai bộ điều khiển nhỏ hơn

Dependency injection & Controllers

Xây dựng tiêm

Bộ chứa dịch vụ Laravel được sử dụng để giải quyết tất cả các bộ điều khiển của Laravel. Do đó, bạn có thể gõ gợi ý bất kỳ phụ thuộc nào mà bộ điều khiển của bạn có thể cần trong hàm tạo của nó. Các phụ thuộc đã khai báo sẽ tự động được giải quyết và đưa vào phiên bản bộ điều khiển

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

3

Tất nhiên, bạn cũng có thể gõ gợi ý bất kỳ hợp đồng Laravel nào. Nếu vùng chứa có thể giải quyết nó, bạn có thể gõ gợi ý nó. Tùy thuộc vào ứng dụng của bạn, việc đưa các phụ thuộc vào bộ điều khiển của bạn có thể mang lại khả năng kiểm tra tốt hơn

phương pháp tiêm

Ngoài việc tiêm hàm tạo, bạn cũng có thể phụ thuộc kiểu gợi ý vào các phương thức của bộ điều khiển của mình. Một trường hợp sử dụng phổ biến để đưa phương thức vào là đưa phiên bản

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

09 vào các phương thức trong bộ điều khiển của bạn

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

4

Nếu phương thức điều khiển của bạn cũng đang mong đợi đầu vào từ một tham số tuyến đường, chỉ cần liệt kê các đối số tuyến đường của bạn sau các phụ thuộc khác của bạn. Ví dụ: nếu tuyến đường của bạn được xác định như vậy

Bạn vẫn có thể nhập gợi ý

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

09 và truy cập tham số

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

21 của mình bằng cách xác định phương thức điều khiển của bạn như sau

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class ShowProfile extends Controller

* Show the profile for the given user.

public function __invoke[$id]

return view['user.profile', ['user' => User::findOrFail[$id]]];

5

Bộ nhớ đệm định tuyến

{note} Không thể lưu trữ các tuyến dựa trên kết thúc. Để sử dụng bộ nhớ đệm định tuyến, bạn phải chuyển đổi bất kỳ Định tuyến đóng nào thành các lớp trình điều khiển

Nếu ứng dụng của bạn chỉ sử dụng các tuyến dựa trên bộ điều khiển, bạn nên tận dụng bộ đệm tuyến của Laravel. Sử dụng bộ đệm tuyến đường sẽ giảm đáng kể lượng thời gian cần thiết để đăng ký tất cả các tuyến đường của ứng dụng của bạn. Trong một số trường hợp, đăng ký tuyến đường của bạn thậm chí có thể nhanh hơn tới 100 lần. Để tạo bộ đệm tuyến đường, chỉ cần thực hiện lệnh Artisan

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

22

Sau khi chạy lệnh này, tệp tuyến đường được lưu trong bộ nhớ cache của bạn sẽ được tải theo mọi yêu cầu. Hãy nhớ rằng, nếu bạn thêm bất kỳ tuyến đường mới nào, bạn sẽ cần tạo bộ đệm tuyến đường mới. Do đó, bạn chỉ nên chạy lệnh

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class UserController extends Controller

* Show the profile for the given user.

return view['user.profile', ['user' => User::findOrFail[$id]]];

22 trong quá trình triển khai dự án của mình

Chúng ta có thể gọi hàm từ bộ điều khiển khác trong laravel không?

Trong Laravel, đôi khi có thể cần truy cập phương thức của bộ điều khiển từ bộ điều khiển khác. Điều này rất dễ thực hiện bằng cách chỉ cần đưa bộ điều khiển có phương thức cần thiết vào bộ điều khiển cần truy cập nó .

Làm cách nào chúng ta có thể gọi phương thức điều khiển từ JavaScript trong lõi mạng asp?

Phương thức Hành động của Trình điều khiển được gọi là sử dụng yêu cầu AJAX JavaScript XmlHttpRequest [XHR] và giá trị của Hộp văn bản được truyền dưới dạng tham số và giá trị được trả về .

Bộ điều khiển hoạt động như thế nào trong laravel?

Bộ điều khiển có thể nhóm logic xử lý yêu cầu có liên quan thành một lớp duy nhất . Ví dụ: một lớp UserController có thể xử lý tất cả các yêu cầu đến liên quan đến người dùng, bao gồm hiển thị, tạo, cập nhật và xóa người dùng. Theo mặc định, bộ điều khiển được lưu trữ trong thư mục ứng dụng/Http/Bộ điều khiển.

Có bao nhiêu loại bộ điều khiển trong laravel?

2. Bộ điều khiển tài nguyên

Chủ Đề