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 .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
76    130    6    26-06-2024
251    86    1    26-06-2024
Đã 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.