Tìm hiểu về la ra vel

14 56 0
Tìm hiểu về la ra vel

Đ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

I.Cài đặt Laravel: +Cài đặt Composer: dung để quản lý  Vào http://getcomposer.org/download/ download composer +Cài đặt Laravel: Hướng dẫn cài đặt laravel thông qua composer  Cài đặt laravel dùng composer create_probject command viết lệnh: composer create-project laravel/laravel [project_folder] [ prefer-dist] +project_folder: tên thư mục dùng để cài đặt laravel Yêu cầu hệ thống:  PHP >=5.3.7  MCrypt PHP Extension II.Cấu hình Laravel  File cấu hình chứa thư mục: app/config  Cách truy cập giá trị file cấu hình Ex: config::get( ‘app.timezone’); $timezone = Config::get(‘app.timezone’,’UTC’); giá trị cấu hình khơng tồn  Thiết lập giá trị file cấu hình Ex: config::get(‘database.default’,’sqlite’); III.Cách thức chạy laravel Những ứng dụng chứa app/start gồm có: global.php, local.php, artisan.php IV.Routing: định tuyến trang web (vào vậy) Các định nghĩa routing chứa app/routes.php A Routing bacsic: + Get/Post Route: Route::get( ‘/ ‘,function() { Return “hello word”; }); Route::post(‘foo/bar’,function() { Return “ hello wold”; }); Ex:Route::get(‘/’, array(‘1’=> ‘loc’,’2’=>function() { Return array[1]; })); +Đăng ký route để áp ứng cho http Route::any(‘foo’,function(){ Return “hello word”;}); +Lưu đường đẫn route thành https Route::get(‘foo1’, array(‘https’,function() { return “mac dinh”; })); +Tạo url route dùng: $url = URL:: to(‘foo1’); B Truyền tham số cho route Route::get(‘user/{id}’,function($id) { Return ‘user’.$id; }); Có thể truyền khơng truyền tham biến Route::get(‘name/{name?}’,function($name = null) { Return $name; }); C Route Filters: kiểm tra liệu đầu vào Route::filter(‘old’,function() { If(input::get(‘age’) ’age:2,4,3’,function() { Return “thu coi”; })); Mơ hình dựa lọc: Route::filter(‘admin’,function() {}); Route:when(‘admin/*’,’admin’); Đặt tên cho Router $url = URL::route(‘ten’); $redirect = Redirect::route(‘poject’); Route Groups: Sub-Domain Routing Ràng buộc Model Route V/Requests & Input Lấy giá trị nhập vào : +Input::get(‘ten’); Ex:$name=Input::get(‘name’); $name=Input::get(‘name’,’thuan’); Lấy giá trị nhập vào có quan hệ cha- con: +Input::has(‘name’) Lấy tất giá trị nhập vào: +Input::all(); Lấy giá trị yêu cầu: +Input::only(‘user’,’pass’) +Input::except(‘credit_card’); Lấy file upload: Input::file(‘photo’); Lấy đường dẫn file upload $path = Input::file(‘photo’)->getRealPath(); Lấy tên file đường dẫn:getClientOriginalName V/ Views : thường chứa html biểu diễn giao diện người dùng có thư mục app/views Truyền liệu cho view Ex:hello.php Controller.php $view = View::make(‘hello’,array(‘name’ =>’thuan’)); Return $view; Truyền liệu cho sub view: Ex: demo/thucoi.php

Thu coi nao

Controller.php $view = View::make(‘hello’)->nest(‘demo1’,’demo.thucoi’); Hello.php View Composers: Khai báo View Composers View::composer(‘hello’,function($view) { $view->with(‘count’,user::count()); }); Có thể khai báo nhiều View View::composer(array(‘hello’,’hello1’),function() { $view->with(‘count’,user::count()); } Responses: Khai báo: Controllers Khai báo Controllers PHẦN 2: Cache: Cấu hình cache chứa app/config/cache.php Events Database Cấu hình : File cấu hình database :app/config/database.php Hổ trợ hệ thống database: MySQL, Postgres, SQLite, and SQL Server Câu lệnh MySQL: Sử dụng câu truy vấn: +Lệnh select: DB::select(‘SELECT * FROM table_name WHERE column_name=?’,array(‘column_name’)) +Lênh insert:DB::insert(‘INSERT INTO table_name(column1,column2, ) values(?,?, ),array(giatri1,giatri2, )) +Lệnh update: DB::update(‘UPDATE table_name SET column = ‘giatri’ where column_name=?’,array(‘giatri’)) +Lệnh delete: DB::delete(‘DELETE FROM table_name’) Database transaction: DB::transaction(function(){ DB::table(‘table_name’)->update(array(‘column’ => ‘giatri’)); }); Cách sử dụng hai kết nối :DB::connection(‘database_name’)->select(‘…’); Cách truy cập trường : DB::conection(‘database_name’)->getPdo(); Cách xây dựng câu query Câu lệnh :SELECT +Lấy giá trị tất dòng table: $query = DB::table(‘table_name’)->get() Foreach($query as $table) { Var_dump($table->column); } +Lấy giá trị row table $sql = DB::table(‘table_name’)->where(‘column’,’ten/giatri’)->first(); Var_dump($sql ->column); +Lấy giá trị column row $sql = DB::table(‘table_name’)->where(‘column’,’ten/giatri’)->pluck(‘column’); +Lấy danh sách giá trị cột table $sql1 = DB::table(‘table_name’)->lists(‘column_name’) +Những câu truy vấn select đặc biệt $sql = DB::table(‘table_name’)->select(‘column1’,’column2’)-get(); $sql = DB::table(‘table_name’)->select(‘ column1 as name’)->get(); $sql = DB::table(‘table_name’)->distinct()->get(); +Thêm column vào table $sql = DB::table(‘table_name’)->select(‘column1’); $sql1 = DB::table(‘table_name’)->addselect(‘column_new’)->get(); +Câu điều kiện where: Toán tử: $sql = DB::table(‘table_name’)->where(‘column’,’>’,100)->get(); OR: $sql = DB::table(‘table_name’)->where(‘column’,’>’,100) ->orwhere(‘column’,’name’) ->get(); +Between $sql = DB::table(‘table_name’)->whereBetween(‘column’,array(1,100))->get(); +Câu điều kiện có In $query = DB::table(‘table_name’)->whereIn(‘id_column’,array(1,2,3))->get(); $query = DB::table(‘table_name’)->whereNotIn(‘id_column’,array(1,2,3))->get(); +Câu điều kiên Null: $query = DB::table(‘table_name’)->whereNull(‘column_name’)->get(); +OrderBy,gruopBy and having $query = DB::table(‘table_name’)->orderBy(‘column_name’,’desc’) ->groupBy(‘column_name’) ->having(‘colum_name’,’>’,giatri) ->get() +Offset and Limit $query = DB::table(‘table_name’)->skip(10)->take(5)->get() B1: Cấu hình database: app/config/database.php B2:Tạo table cách sử dụng migrations + php artisan migrate:install +php artisan migrate:makecreate_users_table + Tạo recode table classCreate_Users_Table { public function up() { Schema::table('users', function($table) { $table->create(); $table->increments('id'); $table->string('email'); $table->string('real_name'); $table->string('password'); $table->timestamps(); }); } /** * Revert the changes to the database * * @return void */ public function down() { Schema::drop('users'); } } php artisan migrate php artisan migrate:rollback php artisan migrate B3:Tạo Eloquent user model class User extends Eloquent { public static $table = 'my_name_table'; } B4:Dùng routing to a closure: application/routes.php Method: get(),post(), put(), delete().Thường dùng phương thức get() and post().Method get() click vào link or mot link tren địa Khi sumbit form dùng phương thức post() EX: Route::get(‘todo’, function() { Return “test route”; }); B5:Tạo table với Eloquent B6:Tạo controller: app/controller B7: Creating the users index view Validator: Kiểm tra liệu đầu vào form, database models, ) Ex: $validator = Validator::make( array(‘name’ => ‘Thuan’), array(‘name’ => ‘required|min:5’)); // kiem tra loi if($validator->fails()) { Return Redirect::back()->with_input()->with_errors($validator); } Bảo mật laravel: Laravel cung cấp cho class để bảo mật thông tin người dùng thông qua Authentication User: login, logout, hạn chế số lượng người truy cập không xác thực tài khoản Một số phương thức Authentiction User sử dụng: Auth::check() : Kiểm tra người dùng login chưa trả true ngược lại flase Auth::guest(): Kiểm tra người dùng chua login trả true Auth::user() : Dùng để lấy giá trị người dùng Auth::login(): Phương thức login có sẵn Auth::logout(): Phương thức thoát đăng nhập người dùng Auth::attempt(array $credentials = array(), $remember = false) : Chứng thực người dùng với thông tin đăng nhập, $tham số truyền vào xác định người dùng có lưu vào hệ thống) Ex: Auth::attempt(array(‘email’ => $email, ‘password’ => $password) Phương thức mã hóa password: Hash::make(‘pasword’) Xác định password băm trở lại Hash::check(‘password’, $password) Kiểm tra password băm trở lại Hash::needsRehash($password) Ngồi laravel cung cấp cho người dùng CSRF : Csrf_token() Route::post(‘register’, array(‘before’ => ‘csrf’, function() { })); Package : A Tạo creating A package: Dùng command để viết lệnh: Php artisan workbench name author/name package –resources B Sự kiện packge Hiển thị view package Return View::make(‘package::view.name’) Lấy yếu tố package Return Config::get(‘package::group.option’) C Lấy đường dẫn package +Lấy đường dẫn route into package Function boot() Include _DIR_.’/ / /routes.php D Cấu hình Package Config::get(‘package::file.optin’) Or config::get(‘package::optin’) Session : Laravel framework đơn giản dễ sử dụng cho việc phát triển web php Laravel giúp cho tạo ứng dụng tuyệt vời với cú pháp dễ hiểu, phong phú.Ưu điểm được thể qua điểm đây: Laravel Vaditation required : bắt buộc nhập giá trị vào text alpha : giá trị bắt buộc chữ alpha_num : giá trị đầu vào bắt buộc số alpha_dash : giá trị đầu vào phải số vào chữ, dấu – or dấu / size : chiều dài cố định so sánh số between:giới hạn số min: giá trị số nhỏ giá trị cung cấp vd: ‘min:1’ max: giá trị cho thấp giá trị cung cấp numeric: giá trị số integer: giá trị đầu vào integer in: giá trị chứa khoảng cho phép not_in: giá trị ko chứa khoảng cho phép confirmed: giá trị kiểm tra thơng tin khóa tồn giá trị mặc định accepted: giá trị kiểm tra giá trị gán cho ‘yes’ or 1(thuận lơi cho việc kiểm tra checkbox) same: giá trị gần giống thuộc tính giá trị có Different: giá trị khác với giá trị có Match: giá trị mặt định biểu thức unique : kiểm tra giá trị bảng exists: giá trị tồn table before : giá trị ngày trước ngày cho after: giá trị ngày sau ngày cho email: giá trị mặc định email url: giá trị mặc định url active_url: giá trị mặc định url hoạt động mimes: kiểm tra kiểu upload file, định nghĩa config/mimes.php image : file hình ảnh Laravel: cung cấp cho thuộc tính để bắt lỗi là: Validator->fails() Validator>passes(), thự viện thu thập tất lỗi thơng qua Validator->errors ... Session : Laravel framework đơn giản dễ sử dụng cho việc phát triển web php Laravel giúp cho tạo ứng dụng tuyệt vời với cú pháp dễ hiểu, phong phú.Ưu điểm được thể qua điểm đây: Laravel Vaditation... if($validator->fails()) { Return Redirect::back()->with_input()->with_errors($validator); } Bảo mật laravel: Laravel cung cấp cho class để bảo mật thông tin người dùng thông qua Authentication User: login, logout,... Hash::check(‘password’, $password) Kiểm tra password băm trở lại Hash::needsRehash($password) Ngồi laravel cung cấp cho người dùng CSRF : Csrf_token() Route::post(‘register’, array(‘before’ => ‘csrf’, function()

Ngày đăng: 25/09/2019, 22:16

Từ khóa liên quan

Mục lục

  • Package :

  • A. Tạo creating A package:

  • Dùng command để viết lệnh:

  • Php artisan workbench name author/name package –resources

  • B. Sự kiện của packge

  • Hiển thị một view trong package

  • Return View::make(‘package::view.name’)

  • Lấy yếu tố của package

  • Return Config::get(‘package::group.option’)

  • C. Lấy đường dẫn của package

  • +Lấy đường dẫn route into package

  • Function boot()

  • Include _DIR_.’/../../routes.php

  • D. Cấu hình Package

  • Config::get(‘package::file.optin’)

  • Or config::get(‘package::optin’)

  • Session :

  • Laravel Vaditation

  • required : bắt buộc nhập giá trị vào text

Tài liệu cùng người dùng

Tài liệu liên quan