在php中如何學(xué)習(xí)laravel框架(菜鳥初學(xué)者)

在php中如何學(xué)習(xí)laravel框架(菜鳥初學(xué)者)

關(guān)于laravel的介紹就不講了,總之laravel是款比較強(qiáng)大的框架,它是國(guó)外框架所以在安裝的上面可能比較麻煩。

laravel的安裝

首先安裝laravel之前要安裝composer,如果是linux系統(tǒng)即可直接下載安裝,下載完后不能安裝記得修改下文件權(quán)限用命令chmod,這邊主要講下window下如何使用composer這個(gè)工具。?

首先百度搜索中國(guó)composer鏡像,就可以找到composer config -g repositories.packagist composer?http://packagist.phpcomposer.com這條命令,運(yùn)行cmd在命令行運(yùn)行上面的命令,就可以下載composer工具,

立即學(xué)習(xí)PHP免費(fèi)學(xué)習(xí)筆記(深入)”;

下載成功后可以看到composer文件底下有個(gè)composer.json文件這是一個(gè)配置文件,打開配置文件寫明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)境變量)。?

laravel的目錄結(jié)構(gòu)介紹?

? ?安裝完的第一次肯定是要想怎么去運(yùn)行它,很簡(jiǎn)單,直接進(jìn)入public文件就可以打開一個(gè)開始頁(yè)面,如果在本地的話那就是localhost/laravelproject/public,就可以運(yùn)行。

接下來(lái)介紹下laravel目錄結(jié)構(gòu),首先介紹下public的index.php文件 里面主要是加載了開始文件然后才能成功運(yùn)行l(wèi)aravel,具體的兩個(gè)文件你可以在根目錄下bootstrap文件夾中找到。現(xiàn)在看下app中的結(jié)構(gòu):?

在php中如何學(xué)習(xí)laravel框架(菜鳥初學(xué)者)
view中主要放的是視圖文件(創(chuàng)建文件時(shí)要用到blade模板,比如創(chuàng)建test.blade.php,laravel中是結(jié)合blade模板引擎來(lái)調(diào)用視圖模板)

controller放的是控制器(手動(dòng)創(chuàng)建時(shí)記得要用composer 命令進(jìn)行更新)

config中主要是配置文件(比如配置數(shù)據(jù)庫(kù)時(shí)要用到database.php文件)

models主要是放模型(也就是數(shù)據(jù)庫(kù)的表)

routes則是路由配置,

filters則是過濾器。?

laravel是怎么運(yùn)行的?

剛學(xué)習(xí)時(shí)肯定是要先嘗試下如何運(yùn)行這個(gè)laravel,首先手動(dòng)創(chuàng)建一個(gè)controller,文件命名為TestController.php,然打開命令行進(jìn)入項(xiàng)目的根目錄下 執(zhí)行 composer dumpautoload,里面內(nèi)容可以模仿homeController.php。

然后編輯routes.php文件,將原來(lái)的Route::GET(‘/’,function()…);修改為Route::Get(‘/’,’TestController@showWelcome’); 然后運(yùn)行也會(huì)跳到laravel歡迎界面。

如果Route::Get(‘test’,’TestController@showWelcome’);則在網(wǎng)站根目錄下后面直接增加test就可以訪問了,到了這里應(yīng)該明白了怎么到Controller,Controller怎么到View了。??

laravel數(shù)據(jù)庫(kù)配置

這邊用到的是mysql,進(jìn)行了簡(jiǎn)單的配置

'mysql'?=>?array( 'driver'????=>?'mysql', 'host'??????=>?'localhost', 'database'??=>?'oss', 'username'??=>?'root', 'password'??=>?'', 'charset'???=>?'utf8', 'collation'?=>?'utf8_unicode_ci', 'prefix'????=>?'', )

laravel的數(shù)據(jù)庫(kù)使用

數(shù)據(jù)表比較多時(shí)且數(shù)據(jù)表的前綴不一樣,則可以先配置模型model,在models文件夾中建立一個(gè)文件要與表名一樣的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 = &#39;users&#39;;     /**      * The attributes excluded from the model&#39;s JSON form.      *      * @var array      */     protected $hidden = array(&#39;password&#39;, &#39;remember_token&#39;); }

即可以直接使用 User ::all() 查詢所有結(jié)果 ?,User::find(2)查詢一個(gè),Post::findOrFail(2) ?

如果沒找到就會(huì)返回錯(cuò)誤,Post::save()、Post::where()->find()、Post::add()、Post::delete()

數(shù)據(jù)庫(kù)的簡(jiǎn)便操作: ?

DB::table(‘tablename’)-&gt;insert([ ????????插入多個(gè)時(shí)要再加一個(gè)數(shù)組 ????????['title'=&gt;'title','name'=&gt;'name'] ????????['title'=&gt;'title'] ????????['title'=&gt;'title'] ????????]) ????????插入時(shí)要想得到ID ????????DB::table('tablename')-&gt;insertGetId(['title'=&gt;'titles']) ????????更新數(shù)據(jù)要有ID ????????DB::table('tablename')-&gt;where('id',1)-&gt;update(['title'=&gt;'titles']) ????????刪除數(shù)據(jù) ????????DB::table('tablename')-&gt;where('id',1)-&gt;delete(); ????????查詢數(shù)據(jù) ????????DB::table('tablename')-&gt;get();??得到全部的值 ????????DB::table('tablename')-&gt;get(['title']);?只查詢title的值 ????????DB::table('tablename')-&gt;first();??只拿第一個(gè) ????????DB::table('tablename')-&gt;orderBy('id','desc')-&gt;first();?根據(jù)id排序 ????????DB::table('tablename')-&gt;where('id','!=',2)-&gt;get();?不等于2 ????????DB::table('tablename')-&gt;where('id','!=',2)-&gt;where('id','&gt;',5)-&gt;get();?可以使用多個(gè)where ????????DB::table('tablename')-&gt;where('id','!=',2)-&gt;OrWhere('id','&gt;',5)-&gt;get();?或者 ????????DB::table('tablename')-&gt;whereBetween('id',[2,5])-&gt;get();??閉包之間 ????????DB::table('tablename')-&gt;whereIn('id',[2,5,9])-&gt;get(); ????????DB::table('tablename')-&gt;whereNotIn('id',[2,5,9])-&gt;get(); ????????DB::table('tablename')-&gt;whereNull('id')-&gt;get();??為空的話就可以查詢出來(lái) ????????DB::table('tablename')-&gt;take(3)-&gt;get();??只查詢3個(gè) ????????DB::table('tablename')-&gt;limit(3)-&gt;get();??只查詢3個(gè) ????????DB::table('tablename')-&gt;skip(2)-&gt;take(3)-&gt;get();??只查詢3個(gè)跳過第二個(gè) ????????DB::table('tablename')-&gt;where('id','!=',2)-&gt;pluck('title');?只返回它的title ????????DB::table('tablename')-&gt;count();??有多少條記錄 ????????DB::table('tablename')-&gt;max('id'); ????????DB::table('tablename')-&gt;min('id'); ????????DB::table('tablename')-&gt;avg('id'); ????????DB::table('tablename')-&gt;sum('id');

多表關(guān)聯(lián)?

在Post中定義?

public?function?comment(){?return?$this-&gt;hasMany('Comment','post_id')?} ?正向關(guān)聯(lián)???一對(duì)多???一對(duì)一是hasOne

在Comment中定義

public?function?post(){?return?$this-&gt;belongsTo('Post','post_id')?} ??反向關(guān)聯(lián)

取得關(guān)聯(lián)值

????Post::find(2)-&gt;comment??就可以得到Comment這張表的內(nèi)容???//這樣查詢一個(gè)是可以的??查詢多個(gè)就要設(shè)置預(yù)載入 ????????????查詢多個(gè) ????????????????Post::with('comment')-&gt;get(); ????????????????Post::with(['comment'=&gt;function($query){$query-&gt;where('id','&gt;',2)}])-&gt;get();??加條件

感謝大家的閱讀,希望大家有所收益。

本文轉(zhuǎn)自:https://blog.csdn.net/Happy_CSDN/article/details/49363219

推薦教程:《php教程

以上就是在php中如何學(xué)習(xí)

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊6 分享