Các câu lệnh cơ bản:
- Xem version php
1 2 | php -v |
- Xem đường dẫn php
1 2 | which php |
sau câu lệnh này, thấy đường dẫn dạng: /usr/bin/php
thì kiểu này dùng mặc định php đã setup vào trong máy.
thay đổi mặc định bằng cách vào ~/.bash_profile
edit lại đường dẫn dạng như sau: export PATH=/Applications/XAMPP/xamppfiles/bin:$PATH
Sau khi edit xong, gọi lệnh source ~/.bash_prpfile
để apply thay đổi.
Debug:
Debug log in each view
1 2 | tail -f storage/logs/laravel.log & php artisan serve |
Debug sql query:
1 2 3 | http://localhost/phpmyadmin/server_variables.php -> Enable genrate log -> ON |
Eloquent ORM trong Laravel
- Mỗi bảng trong csdl tương ứng với 1 model.
- Model cho phép query, insert, update table trong db.
Lệnh tạo 1 model:
1 2 3 4 5 6 7 | php artisan make:model Flight // muon tao db luon php artisan make:model Flight --migration php artisan make:model Flight -m |
Facade:
What ?
- Facade cung cấp 1 static interface cho các Class sẵn có trong service container.
- Larvel cung cấp hầu hết đầy đủ các facade để làm việc với các features của Laravel.
vd:
1 2 3 4 5 6 | use IlluminateSupportFacadesCache; Route::get('/cache', function () { return Cache::get('key'); }); |
Khi nào sử dụng ?
- Có nhiều lợi ích như: ngắn gọn, dễ nhớ để sử dụng hết tất cả các features của Laravel mà ko phải nhớ hết các Class có tên rất dài.
- Sử dụng để dễ dàng tạo trong việc testing. ( dạng interface)
Facade & Dependence Injection:
- facades use dynamic methods to proxy method calls to objects resolved from the service container, we actually can test facades just as we would test an injected class instance.
ex:
1 2 3 4 5 6 | use IlluminateSupportFacadesCache; Route::get('/cache', function () { return Cache::get('key'); }); |
Viết test để verify Cache::get:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | use IlluminateSupportFacadesCache; /** * A basic functional test example. * * @return void */ public function testBasicExample() { Cache::shouldReceive('get') ->with('key') ->andReturn('value'); $this->visit('/cache') ->see('value'); } |
Facade work ntn?
- The Facade base class makes use of the
__callStatic()
magic-method to defer calls from your facade to an object resolved from the container - vd:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php namespace AppHttpControllers; use AppHttpControllersController; use IlluminateSupportFacadesCache; class UserController extends Controller { /** * Show the profile for the given user. * * @param int $id * @return Response */ public function showProfile($id) { $user = Cache::get('user:'.$id); return view('profile', ['user' => $user]); } } |
Blade Templates trong Laravel:
What?
Blade là công cụ tạo khuôn mẫu đơn giản nhưng mạnh mẽ được cung cấp với Laravel. Ko giống như các template php engine khác, Blade ko hạn chế sử dụng code PHP trong view. Các đoạn mã Blade thì được biên dịch thành code PHP thuần & lưu vào bộ nhớ cache cho đến khi nó được thay đổi. Blade view sử dụng : .blade.php
nằm trong thư mục resources/views
Kế thừa template ( Template Inheritance):
Tạo 1 layout
2 Mục đích quan trong khi sử dụng blade : kế thừa template & sections. Xem vd:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> Stored in resources<span class="token operator">/</span>views<span class="token operator">/</span>layouts<span class="token operator">/</span>app<span class="token punctuation">.</span>blade<span class="token punctuation">.</span>php <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>html<span class="token operator">></span> <span class="token operator"><</span>head<span class="token operator">></span> <span class="token operator"><</span>title<span class="token operator">></span>App Name <span class="token operator">-</span> @<span class="token keyword">yield</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'title'</span><span class="token punctuation">)</span><span class="token operator"><</span><span class="token operator">/</span>title<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>head<span class="token operator">></span> <span class="token operator"><</span>body<span class="token operator">></span> @<span class="token function">section</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'sidebar'</span><span class="token punctuation">)</span> This is the master sidebar<span class="token punctuation">.</span> @show <span class="token operator"><</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token double-quoted-string string">"container"</span><span class="token operator">></span> @<span class="token keyword">yield</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'content'</span><span class="token punctuation">)</span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>html<span class="token operator">></span> |
Ở 1 template khác sẽ khai báo 1 section content
thì content sẽ được vẽ ngay đoạn yield
section : định 1 nội dung
yield: dùng để hiển thị nội dung của section.
Mở rộng cho 1 layout
Khi tạo ra 1 view con, sử dụng Blade @extends
để chỉ định bố cục mà view con sẽ kế thừa.
vd :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!-- Stored in resources/views/child.blade.php --> @extends('layouts.app') @section('title', 'Page Title') @section('sidebar') @parent <p>This is appended to the master sidebar.</p> @endsection @section('content') <p>This is my body content.</p> @endsection |
@parent:
để thay thế nội dụng ở thằng view cha.
Components & Slots
1 2 3 4 5 6 | <!-- /resources/views/alert.blade.php --> <div class="alert alert-danger"> {{ $slot }} </div> |
Tạo component để hiển thị trong {{ $slot }}
1 2 3 4 | @component('alert') <strong>Whoops!</strong> Something went wrong! @endcomponent |
multiple slots for a component
1 2 3 4 5 6 7 8 | <!-- /resources/views/alert.blade.php --> <div class="alert alert-danger"> <div class="alert-title">{{ $title }}</div> {{ $slot }} </div> |
Define component
1 2 3 4 5 6 7 8 | @component('alert') @slot('title') Forbidden @endslot You are not allowed to access this resource! @endcomponent |
Note Blade:
{{ }}
: Blade gửi data qua PHP qua chức năng : htmlspecialchars.
{!! $name !!}
: gửi mà ko muốn data escaped.
@if
@elseif
.. điều kiện
1 2 3 4 | @unless (Auth::check()) You are not signed in. @endunless |
1 2 3 4 5 6 7 8 | @isset($records) // $records is defined and is not null... @endisset @empty($records) // $records is "empty"... @endempty |
Include sub-view:
1 2 3 4 5 6 7 8 | <div> @include('shared.errors') <form> <!-- Form Contents --> </form> </div> |
Render View cho Collections:
1 2 | @each('view.name', $jobs, 'job') |