本篇文章給大家分享教你玩轉(zhuǎn)php中l(wèi)aravel框架的學(xué)習(xí)(分享)有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。
關(guān)于laravel的介紹就不講了,總之laravel是款比較強(qiáng)大的框架,它是國外框架所以在安裝的上面可能比較麻煩。
-
laravel的安裝
首先安裝laravel之前要安裝composer,如果是linux系統(tǒng)即可直接下載安裝,下載完后不能安裝記得修改下文件權(quán)限用命令chmod,這邊主要講下window下如何使用composer這個工具。
首先百度搜索中國composer鏡像,就可以找到composer config -g repositories.packagist composer http://packagist.phpcomposer.com
這條命令,運行cmd在命令行運行上面的命令,就可以下載composer工具,下載成功后可以看到composer文件底下有個composer.json文件這是一個配置文件,打開配置文件寫明php版本信息和要下載的laravel信息,格式如下:
{ "name": "laravel/laravel", "description": "The Laravel Framework.", "keywords": ["framework", "laravel"], "license": "MIT", "type": "project", "require": { "php": ">=5.5.9", "laravel/framework": "5.1.*" }, "require-dev": { "fzaninotto/faker": "~1.4", "mockery/mockery": "0.9.*", "phpunit/phpunit": "~4.0", "phpspec/phpspec": "~2.1" }, "autoload": { "classmap": [ "database" ], "psr-4": { "App\": "app/" } }, "autoload-dev": { "classmap": [ "tests/TestCase.php" ] }, "scripts": { "post-install-cmd": [ "php artisan clear-compiled", "php artisan optimize" ], "pre-update-cmd": [ "php artisan clear-compiled" ], "post-update-cmd": [ "php artisan optimize" ], "post-root-package-install": [ "php -r "copy('.env.example', '.env');"" ], "post-create-project-cmd": [ "php artisan key:generate" ] }, "config": { "preferred-install": "dist" }, "repositories": [ {"type": "composer", "url": "http://packagist.phpcomposer.com"}, {"packagist": false} ] }
配置好之后輸入composer install 進(jìn)行安裝laravel,這邊要比較注意的是安裝目錄的路徑問題,如果你想安裝在d盤底下就在把命令行切到d目錄底下進(jìn)行安裝(在此操作之前要配置好環(huán)境變量)。
2. laravel的目錄結(jié)構(gòu)介紹
安裝完的第一次肯定是要想怎么去運行它,很簡單,直接進(jìn)入public文件就可以打開一個開始頁面,如果在本地的話那就是localhost/laravelproject/public,就可以運行。接下來介紹下laravel目錄結(jié)構(gòu),首先介紹下public的index.php文件 里面主要是加載了開始文件然后才能成功運行l(wèi)aravel,具體的兩個文件你可以在根目錄下bootstrap文件夾中找到。現(xiàn)在看下app中的結(jié)構(gòu):
view中主要放的是視圖文件(創(chuàng)建文件時要用到blade模板,比如創(chuàng)建test.blade.php,laravel中是結(jié)合blade模板引擎來調(diào)用視圖模板),controller放的是控制器(手動創(chuàng)建時記得要用composer 命令進(jìn)行更新),config中主要是配置文件(比如配置數(shù)據(jù)庫時要用到database.php文件),models主要是放模型(也就是數(shù)據(jù)庫的表),routes則是路由配置,filters則是過濾器。
3. laravel是怎么運行的
剛學(xué)習(xí)時肯定是要先嘗試下如何運行這個laravel,首先手動創(chuàng)建一個controller,文件命名為TestController.php,然打開命令行進(jìn)入項目的根目錄下 執(zhí)行 composer dumpautoload,里面內(nèi)容可以模仿homeController.php。然后編輯routes.php文件,將原來的Route::GET(‘/’,function()…);修改為Route::Get(‘/’,’TestController@showWelcome’); 然后運行也會跳到laravel歡迎界面。如果Route::Get(‘test’,’TestController@showWelcome’);則在網(wǎng)站根目錄下后面直接增加test就可以訪問了,到了這里應(yīng)該明白了怎么到Controller,Controller怎么到View了。
4. laravel數(shù)據(jù)庫配置
這邊用到的是mysql,進(jìn)行了簡單的配置
'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'oss', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', )
- laravel的數(shù)據(jù)庫使用
數(shù)據(jù)表比較多時且數(shù)據(jù)表的前綴不一樣,則可以先配置模型model,在models文件夾中建立一個文件要與表名一樣的php文件,內(nèi)容如下:
<?php use IlluminateAuthUserTrait; use IlluminateAuthUserInterface; use IlluminateAuthRemindersRemindableTrait; use IlluminateAuthRemindersRemindableInterface; class User extends Eloquent implements UserInterface, RemindableInterface { use UserTrait, RemindableTrait; /** * The database table used by the model. * * @var string */ protected $table = 'users'; /** * The attributes excluded from the model's JSON form. * * @var array */ protected $hidden = array('password', 'remember_token'); }
即可以直接使用 User ::all() 查詢所有結(jié)果 ,User::find(2)查詢一個,Post::findOrFail(2) 如果沒找到就會返回錯誤,Post::save()、Post::where()->find()、Post::add()、Post::delete()
數(shù)據(jù)庫的簡便操作: DB::table(‘tablename’)->insert([
插入多個時要再加一個數(shù)組 ['title'=>'title','name'=>'name'] ['title'=>'title'] ['title'=>'title'] ]) 插入時要想得到ID DB::table('tablename')->insertGetId(['title'=>'titles']) 更新數(shù)據(jù)要有ID DB::table('tablename')->where('id',1)->update(['title'=>'titles']) 刪除數(shù)據(jù) DB::table('tablename')->where('id',1)->delete(); 查詢數(shù)據(jù) DB::table('tablename')->get(); 得到全部的值 DB::table('tablename')->get(['title']); 只查詢title的值 DB::table('tablename')->first(); 只拿第一個 DB::table('tablename')->orderBy('id','desc')->first(); 根據(jù)id排序 DB::table('tablename')->where('id','!=',2)->get(); 不等于2 DB::table('tablename')->where('id','!=',2)->where('id','>',5)->get(); 可以使用多個where DB::table('tablename')->where('id','!=',2)->OrWhere('id','>',5)->get(); 或者 DB::table('tablename')->whereBetween('id',[2,5])->get(); 閉包之間 DB::table('tablename')->whereIn('id',[2,5,9])->get(); DB::table('tablename')->whereNotIn('id',[2,5,9])->get(); DB::table('tablename')->whereNull('id')->get(); 為空的話就可以查詢出來 DB::table('tablename')->take(3)->get(); 只查詢3個 DB::table('tablename')->limit(3)->get(); 只查詢3個 DB::table('tablename')->skip(2)->take(3)->get(); 只查詢3個跳過第二個 DB::table('tablename')->where('id','!=',2)->pluck('title'); 只返回它的title DB::table('tablename')->count(); 有多少條記錄 DB::table('tablename')->max('id'); DB::table('tablename')->min('id'); DB::table('tablename')->avg('id'); DB::table('tablename')->sum('id');
多表關(guān)聯(lián)
在Post中定義 public function comment(){ return $this->hasMany('Comment','post_id') }
正向關(guān)聯(lián) 一對多 一對一是hasOne
在Comment中定義 public function post(){ return $this->belongsTo('Post','post_id') }
反向關(guān)聯(lián)
取得關(guān)聯(lián)值 Post::find(2)->comment 就可以得到Comment這張表的內(nèi)容 //這樣查詢一個是可以的 查詢多個就要設(shè)置預(yù)載入 查詢多個 Post::with('comment')->get(); Post::with(['comment'=>function($query){$query->where('id','>',2)}])->get(); 加條件
推薦學(xué)習(xí):《PHP視頻教程》