TAILIEUCHUNG - Bài giảng Phát triển phần mềm nguồn mở: Bài 8 - Nguyễn Hữu Thể

Trong mô hình MVC thì project sẽ được chia ra làm 3 phần (Model, View, Controller) và trong Laravel framework cũng chia làm 3 phần như vậy. Ở các bài giảng trước đã giới thiệu với các bạn về view trong Laravel, ở phần này sẽ giới thiệu với các bạn về controllers, request, response, session trong Laravel. . | Bài giảng Phát triển phần mềm nguồn mở: Bài 8 - Nguyễn Hữu Thể PHÁT TRIỂN PHẦN MỀM NGUỒN MỞ CONTROLLERS, REQUEST, RESPONSE, SESSION Nguyễn Hữu Thể Controllers − Introduction − Basic Controllers • Defining Controllers • Controllers & Namespaces • Single Action Controllers 2 1 View: Controllers − Controllers có thể nhóm các xử lý request logic vào một class. − Thư mục Controllers: app/Http/Controllers ❖ Tạo Controller: php artisan make:controller --plain 5 Controllers namespace App\Http\Controllers; use App\User; use App\Http\Controllers\Controller; class UserController extends Controller { /** * Show the profile for the given user. * * @param int $id * @return Response */ public function show($id) { return view('', ['user' => User::findOrFail($id)]); } } 6 Controllers ▪ Định nghĩa một route cho action của controller Route::get('user/{id}', 'UserController@show'); ▪ Khi request với route URI, phương thức show của class UserController sẽ được thực thi. 7 Action Controllers __invoke(): Định nghĩa một controller xử lý duy nhất một action namespace App\Http\Controllers; use App\User; use App\Http\Controllers\Controller; class ShowProfile extends Controller { public function __invoke($id) { return view('', ['user' => User::findOrFail($id)]); } } Khi đó bạn đăng ký một route cho một action controllers, bạn không cần xác định phương thức: Route::get('user/{id}', 'ShowProfile'); 8 Example Step 1 − Execute the following command to create UserController. php artisan make:controller UserController --plain Step 2 − After successful execution, you will receive the following output. Step 3 − You can see the created controller at app/Http/Controller/ with some basic coding already written for you and you can add your own coding based on your need. HTTP Requests − Accessing The Request − Request Path .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.