1. Trang chủ
  2. » Công Nghệ Thông Tin

Laravel 8 - Phần 1 - Khởi tạo dự án đầu tiên

15 206 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

Khởi tạo dự án Laravel 8 đầu tiên.

Biên soạn: Nguyễn Hoàng Tùng Giấy phép CC BY-NC 4.0 Quốc tế PHẦN 1: KHỞI TẠO DỰ ÁN LARAVEL 8.x Lab 1: Cài WampServer (Wampserver 3.2.0 64 bit x64) Link: https://www.wampserver.com/en Khi cài đặt nên chọn CSDL Maria DB, không chọn MySQL để tiết kiệm dung lượng ổ cứng Lab 2: Cài đặt Composer (Composer 2.x) Link: https://getcomposer.org/download Nhớ chọn đường dẫn đến tập tin php.exe có phiên phù hợp với Laravel 8.x Lab 3: Cài đặt Laravel 8.x Chuyển tới thư mục www Link: https://laravel.com/docs/8.x#via-composer-create-project Lệnh cài: composer create-project laravel/laravel larashop Lab 4: Bỏ “public” khỏi đường dẫn Đổi tên tập tin server.php thư mục thành index.php Sao chép tập tin htaccess thư mục public ngồi thư mục Bình thường, sử dụng HTTP Server Apache để chạy dịch vụ, không muốn bỏ “public” khỏi đường dẫn, muốn sử dụng HTTP Server tích hợp sẵn PHP, dùng lệnh artisan từ dấu nhắc …\www\larashop> php artisan serve Lúc máy chủ start cổng mặc định 8000, truy cập trang web từ địa chỉ: http://127.0.0.1:8000 (nó tương đương với http://127.0.0.1/larashop/public) Màn hình CMD phải bật suốt trình chạy dịch vụ Nếu cổng 8000 bị trùng chạy lại lệnh để đổi cổng khác php artisan serve port=8001 Lab 5: Tạo Model – Controller – Migration lệnh artisan Chuyển tới thư mục www/larashop Các lệnh: php php php php artisan artisan artisan artisan make:model make:model make:model make:model LoaiSanPham -mcr SanPham -mcr DonHang -mcr DonHang_ChiTiet -mcr Ý nghĩa tham số: -m Migration, tạo sẵn tập tin migrations thư mục database/migrations -c Controller, tạo sẵn tập tin controllers thư mục app/Http/Controllers -r Resource, tạo sẵn hàm rỗng bên tập tin controllers Nếu không muốn tạo hàm rỗng bên tập tin controllers bỏ tham số -r Ví dụ: php artisan make:model LoaiSanPham -mc php artisan make:model SanPham -mc Lab 6: Chỉnh sửa tập tin CSDL Migrations vừa tạo từ Lab Mô tả CSDL: - Bảng: loaisanpham STT Tên trường id tenloai tenloai_slug Kiểu liệu int varchar varchar Kích thước 255 255 Ghi Khóa chính, Tự động tăng Not Null Not Null - Bảng: sanpham STT Tên trường id loaisanpham_id tensanpham tensanpham_slug soluong dongia hinhanh motasanpham Kiểu liệu int int varchar varchar int double varchar text Kích thước Kiểu liệu int int varchar varchar tinyint Kích thước Kiểu liệu int int int int double Kích thước 255 255 255 255 Ghi Khóa chính, Tự động tăng Khóa ngoại, Not Null Not Null Not Null Not Null Not Null Allow Null Allow Null - Bảng: donhang STT Tên trường id user_id dienthoaigiaohang diachigiaohang tinhtrang 20 255 Ghi Khóa chính, Tự động tăng Khóa ngoại, Not Null Not Null Not Null Not Null, Default - Bảng: donhang_chitiet STT Tên trường id donhang_id sanpham_id soluongban dongiaban Nội dung tập tin migrations: Các tập tin migrations nằm thư mục database/migrations Laravel tạo sẵn tập tin migrations (users, password_resets, failed_jobs) Xem Lab để xem cách đổi tên bảng mặc định - Loại sản phẩm Ghi Khóa chính, Tự động tăng Khóa ngoại, Not Null Khóa ngoại, Not Null Not Null Not Null public function up() { Schema::create('loaisanpham', function (Blueprint $table) { $table->id(); $table->string('tenloai'); $table->string('tenloai_slug'); $table->timestamp('created_at')->useCurrent(); $table->timestamp('updated_at')->useCurrentOnUpdate(); $table->engine = 'InnoDB'; }); } - Sản phẩm public function up() { Schema::create('sanpham', function (Blueprint $table) { $table->id(); $table->foreignId('loaisanpham_id')->constrained('loaisanpham'); $table->string('tensanpham'); $table->string('tensanpham_slug'); $table->integer('soluong'); $table->double('dongia'); $table->string('hinhanh')->nullable(); $table->text('motasanpham')->nullable(); $table->timestamp('created_at')->useCurrent(); $table->timestamp('updated_at')->useCurrentOnUpdate(); $table->engine = 'InnoDB'; }); } - Đơn hàng public function up() { Schema::create('donhang', function (Blueprint $table) { $table->id(); $table->foreignId('user_id')->constrained('users'); $table->string('dienthoaigiaohang', 20); $table->string('diachigiaohang'); $table->tinyInteger('tinhtrang')->default(0); $table->timestamp('created_at')->useCurrent(); $table->timestamp('updated_at')->useCurrentOnUpdate(); $table->engine = 'InnoDB'; }); } - Đơn hàng chi tiết public function up() { Schema::create('donhang_chitiet', function (Blueprint $table) { $table->id(); $table->foreignId('donhang_id')->constrained('donhang'); $table->foreignId('sanpham_id')->constrained('sanpham'); $table->integer('soluongban'); $table->double('dongiaban'); $table->timestamp('created_at')->useCurrent(); $table->timestamp('updated_at')->useCurrentOnUpdate(); $table->engine = 'InnoDB'; }); } - Người dùng public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamp('created_at')->useCurrent(); $table->timestamp('updated_at')->useCurrentOnUpdate(); $table->engine = 'InnoDB'; }); } Lab 7: Chỉnh sửa thông số kết nối CSDL tập tin env Tập tin env nằm thư mục gốc dự án (thư mục larashop) APP_NAME=LaraShop APP_ENV=local APP_KEY=base64:9KMMj7b+o5FpaBPzUw/P11muXL9U4oH/CE8+H3anEEA= APP_DEBUG=true APP_URL=http://127.0.0.1:8080/larashop LOG_CHANNEL=stack DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=larashop DB_USERNAME=root DB_PASSWORD= Lab 8: Tạo CSDL rỗng phpMyAdmin Vào phpMyAdmin tạo CSDL rỗng có tên giống cấu hình Lab Lab 9: Chạy lệnh artisan để tạo bảng Link: https://laravel.com/docs/8.x/migrations php artisan migrate Vào phpMyAdmin xem lại bảng vừa sinh Sơ đồ quan hệ bảng: Nếu bước có phát sinh lỗi chiều dài kiểu varchar (too long) xem cách fix lỗi link: https://laravel.com/docs/8.x/migrations#index-lengths-mysql-mariadb Lab 10: Tạo form xác thực (Đăng ký, đăng nhập, quên mật khẩu,…) Cài gói UI lệnh: composer require laravel/ui Tạo form xác thực lệnh: php artisan ui:auth Lab 11: Chỉnh giao diện Master - Giao diện master nằm thư mục resources/views/layouts - Nhúng liên kết CSS JS Bootstrap phiên - Liên kết: https://getbootstrap.com/docs/5.0/getting-started/introduction - Chỉnh lại Navbar - Nhúng Font Awesome - Liên kết: https://fontawesome.com - Xem code master đầy đủ Lab 15 bên Lab 12: Chỉnh tập tin Models thư mục app/Models, lưu lý hasMany, belongsTo Laravel tạo sẵn model User.php Xem Lab để xem cách đổi User.php thành tên khác (ví dụ: NguoiDung.php) - Tập tin LoaiSanPham.php class LoaiSanPham extends Model { use HasFactory; protected $table = 'loaisanpham'; // protected $primaryKey = 'id'; // protected $keyType = 'string'; public function SanPham() { return $this->hasMany('App\Models\SanPham', 'loaisanpham_id', 'id'); } } - Tập tin SanPham.php class SanPham extends Model { use HasFactory; protected $table = 'sanpham'; // protected $primaryKey = 'id'; // protected $keyType = 'string'; public function LoaiSanPham() { return $this->belongsTo('App\Models\LoaiSanPham', 'loaisanpham_id', 'id'); } public function DonHang_ChiTiet() { return $this->hasMany('App\Models\DonHang_ChiTiet', 'sanpham_id', 'id'); } } - Tập tin DonHang.php class DonHang extends Model { use HasFactory; protected $table = 'donhang'; // protected $primaryKey = 'id'; // protected $keyType = 'string'; public function User() { return $this->belongsTo('App\Models\User', 'user_id', 'id'); } public function DonHang_ChiTiet() { return $this->hasMany('App\Models\DonHang_ChiTiet', 'donhang_id', 'id'); } } - Tập tin DonHang_ChiTiet.php class DonHang_ChiTiet extends Model { use HasFactory; protected $table = 'donhang_chitiet'; // protected $primaryKey = 'id'; // protected $keyType = 'string'; public function DonHang() { return $this->belongsTo('App\Models\DonHang', 'donhang_id', 'id'); } public function SanPham() { return $this->belongsTo('App\Models\SanPham', 'sanpham_id', 'id'); } } - Tập tin User.php class User extends Authenticatable { use HasFactory, Notifiable; protected $fillable = [ 'name', 'email', 'password', ]; protected $hidden = [ 'password', 'remember_token', ]; protected $casts = [ 'email_verified_at' => 'datetime', ]; public function DonHang() { return $this->hasMany('App\Models\DonHang', 'user_id', 'id'); } } Lab 13: Chỉnh tập tin Controllers Thống tên hàm để dễ copy/paste Ví dụ: getDanhSach, getThem, postThem, getSua, postSua, getXoa - Tập tin: LoaiSanPhamController.php class LoaiSanPhamController extends Controller { public function getDanhSach() { $loaisanpham = LoaiSanPham::all(); return view('loaisanpham.danhsach', compact('loaisanpham')); } public function getThem() { return view('loaisanpham.them'); } public function postThem(Request $request) { $orm = new LoaiSanPham(); $orm->tenloai = $request->tenloai; $orm->tenloai_slug = Str::slug($request->tenloai, '-'); $orm->save(); return redirect()->route('loaisanpham'); } public function getSua($id) { $loaisanpham = LoaiSanPham::find($id); return view('loaisanpham.sua', compact('loaisanpham')); } public function postSua(Request $request, $id) { $orm = LoaiSanPham::find($id); $orm->tenloai = $request->tenloai; $orm->tenloai_slug = Str::slug($request->tenloai, '-'); $orm->save(); return redirect()->route('loaisanpham'); } public function getXoa($id) { $orm = LoaiSanPham::find($id); $orm->delete(); return redirect()->route('loaisanpham'); } } - Tương tự với tập tin khác Dùng thao tác copy/paste sửa lại nội dung hàm cho phù hợp Lab 14: Chỉnh nội dung tập tin web route tương ứng với hàm Controllers (routes/web.php)

Ngày đăng: 16/12/2020, 14:54

TỪ KHÓA LIÊN QUAN

w