laravel文檔1 laravel文檔2javascript
安裝:php
方法1:html
下載:進入要安裝目錄path/www:執行 以下命令 建立一個文件名爲blog的項目 而後composer將laravel的文件下載到此文件java
composer create-project --prefer-dist laravel/laravel blog
下載完畢後執行composer install 安裝laravel的依賴包及自動生成應用程序密鑰linux
方法2:laravel
github.com下載壓縮包解壓到你要安裝的目錄 ,cmd控制檯進入該目錄 而後執行命令composer instalgit
二、安裝完成後 在cmd控制檯輸入: php artisan key:generate 沒有這一步很大可能報錯 in compiled.php line 13331:The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.github
複製根目錄.env.example文件爲.env (window cmd copy .env.example .env | linux系統 cp .env.example .env) web
定位到項目根目錄 輸入 php artisan key:generate 而後key就自動填寫到.env文件裏了正則表達式
固然也能夠複製key到config/app.php文件(雖然沒什麼必要) 'key' => env('APP_KEY','上一部生成的key'),
方法3:
像我同樣換了國內鏡像也慢的不行的 那就下載一鍵安裝包吧 下載地址 解壓便可訪問
出現 file_get_contents(D:\xampp\htdocs\laravel-master/.env): failed to open stream: No such file or directory 給app/storage目錄設置權限便可解決問題chmod -Rvc 777 app/storage
若是有些版本不起做用那麼使用
find app/storage -type d -exec chmod -vc 777 {} \;
find app/storage -type f -exec chmod -vc 777 {} \;
打開開發者調試模式:
出現Whoops, looks like something went wrong 那麼須要打開config/app.php將debug調試以便打印錯誤緣由
配置目錄:
獲取配置信息:
config('app'); #獲取/config/app.php的全部信息 config('app.name'); #獲取/config/app.php的某個信息 config('auth.defaults'); #獲取/config/auth.php的某個信息
動態設置配置信息:
config([ 'app.timezone' => 'America/Chicago', 'app.name'=>'Laravel', 'auth.default'=>[ 'guard' => 'web', 'passwords' => 'users', ] ]);
核心概念:
服務容器、服務提供器、契約 即依賴注入 依賴注入精要
路由:
//在/routes/web.php 中定義路由 //首頁 轉發到/resources/views/welcome.blade.php Route::get('/', function () { return view('welcome'); }); # (get請求) 路由輸出字符串:http://localhost/laravel/public/base Route::get('base', function () { return 'hello world!'; }); # (post請求) 路由輸出字符串:http://localhost/laravel/public/base Route::post('base', function () { return 'hello world!'; }); # 由http://localhost/laravel/public/about的轉發到/resources/views/home/index.blade.php Route::get('about', function () { return view('home.index'); }); # 多請求路由 Route::match(['get','post'],'base2',function(){ return '多請求路由,能夠接受get和post請求'; }); Route::any('base3',function(){ return '接受任何請求' ; }); # 路由參數:http://localhost/laravel/public/user/1 路由傳遞id=1 Route::get('user/{id}',function($id){ return 'user-'.$id; }); # 可選的路由參數 Route::get('user/{name?}',function($name=null){ return 'user-'.$name; }); # 帶默認值的可選的路由參數 Route::get('user/{name?}', function($name = 'John') { return $name; }); # 帶正則表達式約束的路由 Route::get()->where() Route::get('user/{name}', function($name) { return $name; })->where('name', '[A-Za-z]+'); Route::get('user/{id}', function($id) { return $id; })->where('id', '[0-9]+'); #多參數的約束路由//http://localhost/laravel/public/user/2/tom Route::get('user/{id}/{name?}', function($id,$name='sean') { return $id.$name; })->where(['id'=>'[0-9]+','name'=>'[A-Za-z]+']); #前綴+路由羣組 Route::group(['prefix'=>'member'],function(){ # http://localhost/laravel/public/member/user/custom Route::get('user/custom',['as'=>'center', function() { return route('center'); }]); # http://localhost/laravel/public/member/base Route::any('base',function(){ return 'this is base'; }); }); #轉發到控制器方法 # 由http://localhost/laravel/public/member/info的轉發到/app/Http/Controller/MemberController.php 裏的MemberController類的demofunction Route::get('member/info', "MemberController@demofunction"); Route::get('member/info', ['uses'=>"MemberController@demofunction"]); #路由別名: 控制器等地方獲取路由時使用別名就ok route('memberinfo'); Route::get('member/info', [ 'uses'=>"MemberController@demofunction", 'as'=>"memberinfo" #路由別名#指定路由名稱 ]); #轉發到控制器方法 # 由http://localhost/laravel/public/member/2/dash Route::get('member/{id}/{name?}', "MemberController@info") ->where(['id'=>'[0-9]+','name'=>'[A-Za-z]+']) ->name('member'); Route::get('member/{id}/{name?}', [ 'uses'=>"MemberController@info", function() { return route('center'); } ])->where(['id'=>'[0-9]+','name'=>'[A-Za-z]+']) ->name('member');#等同於下面這個: #路由別名: 控制器等地方獲取路由時使用別名就ok route('memberinfo'); Route::get('member/{id}/{name?}', [ 'uses'=>"MemberController@info", 'as'=>"member", #等同於->name('uses') function(){ return route('member'); } ])->where(['id'=>'[0-9]+','name'=>'[A-Za-z]+']);
Route::get('articles',[ 'uses'=>'ArticlesController@index', 'as'=>'articles.index' #等同於name() ]); Route::get('/iocallreport/export-file/{type}', 'Cms\ReportsController@exportFile'); #簡單模式 格式 網站根url+參數 <a href="{{ url('/articles') }}">連接</a> <a href="{{ url('iocallreport/export-file/',['type'=>'xls']) }}">Download Excel xls</a> # or <a href="{{ URL::to('/articles') }}">連接</a> #路由模式 這種方式是指定匹配註冊路由時的 'as' 參數,獲得註冊的uri。 URL::route('articles.index') Route::get('/iocallreport/export-file/{type}', 'Cms\ReportsController@exportFile')->name('iocallreport'); <a href="{{ route('iocallreport',['type'=>'xls']) }}">Download Excel xls</a> #控制器動做模式: 這種方式是根據註冊路由時 'uses' 參數 URL::action('ArticlesController@index') Route::controller() #5.2已廢棄
參數的獲取; 詳情
input('') $request request()
MVC
M:/app/Models (官方讓本身建Models目錄 /app下給了一個參考的User.php)
namespace App\Models; use \Illuminate\Database\Eloquent\Model; class Member extends Model { public static function getMembers(){ return 'members'; } }
V:/resources/views 模板文件:/resources/views/[ooxxDir/]xxoo.blade.php 輸出模板 控制器方法 rerurn view('[ooxxDir/]xxoo');
公共模板
<!DOCTYPE html> <html lang="zh-cn"> <head> <title>this is @yield('title')</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/> <link href="" rel="stylesheet"> <script src="" type="text/javascript"></script> <style> *{ border:0; margin: 0; padding: 0; border: 1px solid red; -moz-box-sizing:border-box; /* Firefox */ -webkit-box-sizing:border-box; /* Safari */ box-sizing: border-box; } .header,.footer{ width: 100%; max-width:1290px; height: 100px; } .main{ margin:0 auto; display: -webkit-flex; /* Safari */ display:flex; flex-flow: row nowrap; /*flex-flow: <flex-direction> <flex-wrap>前兩個排列和換行的縮寫*/ /* flex-direction: row 橫向->爲主軸 默認 flex-direction:row-reverse 橫向<-爲主軸 flex-direction: column 豎向↓爲主軸 flex-direction: column-reverse; 豎向↑爲主軸 flex-wrap: nowrap:超出不換行(默認) | wrap:換行第一行在上方 | wrap-reverse:第一行在下方; */ justify-content: flex-start ; align-items: center; /* justify-content: flex-start | flex-end | center | space-between | space-around; 與主軸對其方式 align-items: flex-start | flex-end | center | baseline | stretch; 交叉軸的對齊方式 */ } .sidebar{ height: 500px; width:200px; } .content{ height: 500px; width:1090px; } </style> </head> <body> <div class="header"> @section('header') 頭部 @show </div> <div class="main"> <div class="sidebar"> @section('sidebar') 側邊欄 @show </div> <div class="content"> @yield('content','主要內容區') </div> </div> <div class="footer"> @section('footer') 底部 @show </div> </body> </html>
子模板
@extends('layouts') @section('title', '重寫的Title') @section('header') @parent 將父模板的也顯示出來 重寫的頭部 @stop @section('sidebar') 重寫的側邊欄 <!--模板輸出變量--> <p>{{$name=1}}</p> <!-- 調動php代碼--> <p>{{time()}}</p> <p>{{date('Y-m-d H:i:s',time())}}</p> <p>{{in_array($name,$arr=[0,1,2])?"true":"false"}}</p> <p>{{isset($name)?$name:"default"}}</p> <p>{{$name or "default"}}</p><!--等同於上面--> <!--原樣輸出--> <p>@{{$name}}</p> <!--包裹原樣輸出的區域 這樣就不用爲每一個須要解析的變量加@了--> @verbatim <div class="container"> Hello, {{ name }}. </div> @endverbatim <!--模板註釋 不輸出到瀏覽器--> <p>{{--看不到我看不到我--}}</p> <!-- 引入子視圖 全部在父視圖的可用變量在被引入的視圖中都是可用的 還能夠傳入二外的變量至子視圖 加載views/component/upload.blade.php 並傳值 --> @include('component.upload',['extradata'=>'elsedata']) 被引入的子視圖不存在,報錯 @includeif('component.upload',['extradata'=>'elsedata']) 被引入的子視圖不存在時不會報錯 <!--流程控制 if--> @if (count($records=[0,1]) === 1) 我有一條記錄! @elseif (count($records) > 1) 我有多條記錄! @else 我沒有任何記錄! @endif @unless (Auth::check()) 當爲false時輸出 @endunless <!--循環--> for、 foreach、while <!--顯示未通過htmlspecialchars轉義過的數據--> <p>Hello, {!! $name !!}.</p> @stop @section('content') 重寫的內容區 @stop @section('footer') 重寫的頁腳 @stop
C:/app/Http/Controller
namespace app\Http\Controllers; use App\Http\Controllers\Controller; use App\Models\Member; /** * this is MemberController */ class MemberController extends Controller { public function info($id,$name) { $member=Member::getMembers(); url('member',['id'=>$id,'name'=>$name]); //var_dump($member); return view('member/info'); } }
數據查詢
laravel提供三種數據庫查詢方式: #一、DB facade(原始查詢) #二、查詢構造器 #三、Eloquent ORM #在config/database.php下配置數據庫而且在.env文件裏填寫數據庫參數 #注意:有表前綴的話也在config/database.php下添加修改'prefix' => 'mdl_', #原始查詢 #DB類在use Illuminate\Support\Facades\DB; 在控制器引入並在控制器方法裏實現增刪改查 DB::insert('insert into mdl_student(name,age) values(?,?)',['tom',18]); //成功返回true DB::delete('delete from mdl_student where id > ?',[0]); //返回行數 DB::update('update mdl_student set age=? where name= ?',[20,'tom']); //成功返回行數 $students=DB::select('select * from mdl_student'); dd($students); #至關於tp的dump #構造器查詢 #增長數據 $res=DB::table('student')->insert(array('name'=>'tom','age'=>18)); //返回布爾 $res=DB::table('student')->insert([['name'=>'tom','age'=>18],['name'=>'lily','age'=>20]]); //返回布爾 $res=DB::table('student')->insertGetId(array('name'=>'tom','age'=>18)); //返回id #修改數據 $res=DB::table('student')->where('id',33)->update(array('age'=>30)); //返回行數 $res=DB::table('student')->increment('age'); //返回行數 全表id自增1 $res=DB::table('student')->increment('age',3); //返回行數 全表id自增3 $res=DB::table('student')->decrement('age'); //返回行數 全表id自減1 $res=DB::table('student')->decrement('age',3); //返回行數 全表id自減3 $res=DB::table('student')->where('id',33)->decrement('age',3); //返回行數 id=33 的行age自減3 $res=DB::table('student')->where('id',33)->increment('age',3,['name'=>'yinli']); //返回行數 id=33 的行age自增3並改變name的值 #刪除數據 $res=DB::table('student')->where('id',33)->delete(); //返回行數 刪除id=33的數據 $res=DB::table('student')->where('id','>=',33)->delete(); //返回行數 刪除id>=33的數據 $res=DB::table('student')->truncate(); //不反回任何數據 清空數據表 慎用!!! #查詢數據: $res=DB::table('student')->where('id','>=',1)->get(); //查詢符合條件的全部數據 $res=DB::table('student')->whereRaw('id >= ? and age > ?',[3,21])->get(); //多條件查詢 $res=DB::table('student')->first(); //返回結果集的第一行數據 $res=DB::table('student')->pluck('name'); //返回結果集特定字段 $res=DB::table('student')->pluck('name','id'); //返回結果集特定字段並制定下標 $res=DB::table('student')->select('id','name','age')->get(); //指定字段 #分段查詢 每次查詢2條數據 return false就會中止查詢後面的數據 DB::table('student')->orderBy('id','asc')->chunk(2,function($res){ if($res[0]->id >= 3){ return false; } }); #指定字段 #聚合查詢: $res=DB::table('student')->count(); //總條數 $res=DB::table('student')->sum('age'); //總和 $res=DB::table('student')->max('age'); //最大 $res=DB::table('student')->min('age'); //最小 $res=DB::table('student')->avg('age'); //平均數 Eloquent ORM #首先新建一個模型\app\Models\Student.php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Student extends Model{ #指定表名(默認是複數 即默認的表名爲students) 表前綴怎麼指定的? protected $table='student'; #指定id (Eloquent 默認每張表的主鍵名爲 id) protected $primaryKey ='id'; //注意在默認狀況下,在數據庫表裏須要有 updated_at 和 created_at 兩個字段。且默認的插入格式爲Y-m-d H:i:s 這樣兩個字段類型必須爲vachar //若是您不想設定或自動更新這兩個字段,則將類裏的 $timestamps 屬性設爲 false便可 public $timestamps = false; /** * 當$timestamps爲true 自動更新時間時自定義的時間格式(默認是Y-m-d H:i:s) * */ protected function getDateFormat() { //將插入的時間換成時間戳 return time(); } /* * 更改返回時間的格式 * $timestamps爲true時 查詢調用($res->created_at)的時間默認格式是將時間戳轉換成 Y-m-d H:i:s 000000 格式 * */ protected function asDateTime($value) { //將查詢的時間換成時間戳(仍是數據庫保存的時間格式?) return $value; } } #在新建一個控制器\app\Http\Controllers\User\StudentController.php namespace App\Http\Controllers\User; use App\Http\Controllers\Controller; use App\Models\Student; //將模型的類引入 class StudentController extends Controller{ public function index(){ #ORM查詢 $res=Student::all();//查詢全部 $res=Student::find(3);//根據主鍵id查找 無數據返回null $res=Student::findOrFail(3);//根據主鍵id查找,無數據拋出致命錯誤! $res=Student::get();//查詢全部 $res=Student::where('id','>','3') ->orderBy('age','desc') ->first();//根據條件查詢一條 Student::chunk(2,function($res){ //print_r($res); });//分段查詢 $res=Student::count(); $res=Student::where('id','>',3)->max('age'); var_dump($res); return 'srudentindex'; } } #路由設置:\routes\web.php Route::any( 'user/student/index', [ 'uses'=>'User\StudentController@index', 'as'=>'student' ] ); #訪問http://localhost/laravel/public/user/student/index便可訪問到StudentController
HTTP層: