今天主要的學習任務有以下
安裝
安裝環境 30’
配置
-
The Basics
安裝
安裝Composer
Compser是PHP依賴管理工具,
curl -sS https://getcomposer.org/installer | php
如果提示HTTPS證書問題可以改用如下命令行安裝以忽略證書
curl -k https://getcomposer.org/installer | php
安裝完Composer后,使用Composer來安裝laravel
php composer.phar global require "laravel/installer=~1.1"
安裝完成后將laravel加到PATH路徑中,在MAC上修改PATH變量 。
添加完了后就可以使用一下命令在當前目錄下創建項目了。
laravel new Project_Name
laravel 要求PHP版本大于等于5.4
在Laravel 5中調整public文件夾的名稱為public_html
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/../bootstrap/app.php';
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
all set, let's start building
配置
所有和應用相關的配置都保存在config
文件夾中
讀取Config變量
$value = Config::get('app.timezone'); // 可以通過外觀模式來讀寫Config里定義的變量
Config::set('app.timezone', 'America/Chicago');
$value = config('app.timezone'); // 也可以通過幫助函數來讀取Config的值
storage
和vendor
文件夾需要寫權限
修改namespace的名稱可以使用如下命令行
php artisan app:name Car
維護模式 - 這個可以有
php artisan down
php artisan up
維護期間的提示頁模板 resources/views/errors/503.blade.php
在項目根目錄下有一個.env文件,在這里定義的所有常量都可以通過$_ENV
訪問。工程師各自的開發環境會有不同,所以這個文件最好不要提交到SourceControl中。
獲取并檢測當前應用的環境
$environment = app()->environment(); // helper function
$environment = App::environment(); // app facade
if ($app->environment('local')) {// The environment is local}
if ($app->environment('local', 'staging')){ // The environment is either local OR staging...}
MARK 文檔里有下面一句沒有看懂什么意思,估計等到學Service Container的時候會搞明白
To obtain an instance of the application, resolve the Illuminate\Contracts\Foundation\Application
contract via the service container.
The Basics
Routing
Laravel的路由做的還是很優雅的,用這套路由機制做RESTful API實在是太爽了(相比CI)
路由分組
這是個很方便的工具,可以對多個路由分組,將
Middleware
這貨讓我想起了很久之前.net framework里的http handler。。。
所有的middleware都存在App/Http/Middleware/這個文件夾中
創建Middleware
執行下面的語句來創建新的Middleware
php artisan make:middleware ImageFetcher
執行成功會在middleware的文件夾中多一個ImageFetcher.php的文件。
<?php namespace App\Http\Middleware;
use Closure;
class ImageFetcher {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// Perform action before request is handled by app
$response = $next($request);
// Perform action after request is handled by app
return $response;
}
}
注冊
Middleware做完了之后需要注冊到應用中去,app/Http/這個文件夾下有Kernal.php文件,全局的middleware注冊在$middleware變量中;如果是要給某個特定的Route綁定使用的middleware,注冊在$routeMiddleware這個變量中。
Route::get('admin/profile', ['middleware' => 'auth', function(){
// do the shit here
}]);
可結束的Middleware
有的操作需要在http請求被處理之后才可以做,例如Session要在http請求結束后才會往客戶端寫數據。碰到這種情況,將middleware聲明為TerminableMiddleware,在termintate這個函數中進行http請求完成后的操作。
use Closure;
use Illuminate\Contracts\Routing\TerminableMiddleware;
class StartSession implements TerminableMiddleware {
public function handle($request, Closure $next)
{
return $next($request);
}
public function terminate($request, $response)
{
// Store the session data...
}
}