今天主要的學(xué)習(xí)任務(wù)有以下
安裝
安裝環(huán)境 30’
配置
-
The Basics
安裝
安裝Composer
Compser是PHP依賴管理工具,
curl -sS https://getcomposer.org/installer | php
如果提示HTTPS證書(shū)問(wèn)題可以改用如下命令行安裝以忽略證書(shū)
curl -k https://getcomposer.org/installer | php
安裝完Composer后,使用Composer來(lái)安裝laravel
php composer.phar global require "laravel/installer=~1.1"
安裝完成后將laravel加到PATH路徑中,在MAC上修改PATH變量 。
添加完了后就可以使用一下命令在當(dāng)前目錄下創(chuàng)建項(xiàng)目了。
laravel new Project_Name
laravel 要求PHP版本大于等于5.4
在Laravel 5中調(diào)整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

配置
所有和應(yīng)用相關(guān)的配置都保存在config文件夾中
讀取Config變量
$value = Config::get('app.timezone'); // 可以通過(guò)外觀模式來(lái)讀寫(xiě)Config里定義的變量
Config::set('app.timezone', 'America/Chicago');
$value = config('app.timezone'); // 也可以通過(guò)幫助函數(shù)來(lái)讀取Config的值
storage 和vendor文件夾需要寫(xiě)權(quán)限
修改namespace的名稱可以使用如下命令行
php artisan app:name Car
維護(hù)模式 - 這個(gè)可以有
php artisan down
php artisan up
維護(hù)期間的提示頁(yè)模板 resources/views/errors/503.blade.php
在項(xiàng)目根目錄下有一個(gè).env文件,在這里定義的所有常量都可以通過(guò)$_ENV 訪問(wèn)。工程師各自的開(kāi)發(fā)環(huán)境會(huì)有不同,所以這個(gè)文件最好不要提交到SourceControl中。
獲取并檢測(cè)當(dāng)前應(yīng)用的環(huán)境
$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 文檔里有下面一句沒(méi)有看懂什么意思,估計(jì)等到學(xué)Service Container的時(shí)候會(huì)搞明白
To obtain an instance of the application, resolve the Illuminate\Contracts\Foundation\Application
contract via the service container.
The Basics
Routing
Laravel的路由做的還是很優(yōu)雅的,用這套路由機(jī)制做RESTful API實(shí)在是太爽了(相比CI)
路由分組
這是個(gè)很方便的工具,可以對(duì)多個(gè)路由分組,將
Middleware
這貨讓我想起了很久之前.net framework里的http handler。。。
所有的middleware都存在App/Http/Middleware/這個(gè)文件夾中
創(chuàng)建Middleware
執(zhí)行下面的語(yǔ)句來(lái)創(chuàng)建新的Middleware
php artisan make:middleware ImageFetcher
執(zhí)行成功會(huì)在middleware的文件夾中多一個(gè)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;
}
}
注冊(cè)
Middleware做完了之后需要注冊(cè)到應(yīng)用中去,app/Http/這個(gè)文件夾下有Kernal.php文件,全局的middleware注冊(cè)在$middleware變量中;如果是要給某個(gè)特定的Route綁定使用的middleware,注冊(cè)在$routeMiddleware這個(gè)變量中。
Route::get('admin/profile', ['middleware' => 'auth', function(){
// do the shit here
}]);
可結(jié)束的Middleware
有的操作需要在http請(qǐng)求被處理之后才可以做,例如Session要在http請(qǐng)求結(jié)束后才會(huì)往客戶端寫(xiě)數(shù)據(jù)。碰到這種情況,將middleware聲明為T(mén)erminableMiddleware,在termintate這個(gè)函數(shù)中進(jìn)行http請(qǐng)求完成后的操作。
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...
}
}