laravel5.3的新功能嚐鮮

 

 

上一節課 https://my.oschina.net/lilugirl2005/blog/783370php

上一節課咱們講了laravel5.3的安裝,這節講laravel5.3的一些基礎命令和配置css

Listeners文件夾

進入laravel項目目錄輸入命令html

建立用戶註冊的事件監聽 看看laravel

php artisan make:listener UserSignUpListener --event=UserSignUp

 

cache helper function

用tinker作一些cache方面的練習web

DB 查詢返回 Collection 

建立初始數據

建立3條假數據

其實你能夠生成更多的測試數據方便後面的分頁功能的測試,因此我又執行了一遍命令生成了30條假數據數據庫

 

DB查詢語句

其餘靈活的方法bootstrap

loop 變量使用 

作一個小例子

修改routes/web.php文件瀏覽器

修改對應的view文件  resources/views/welcome.blade.phpbash

<body>
        <div class="flex-center position-ref full-height">
        <ul>
        @foreach($users as $user)
         <li>{{$loop->index}}{{$user->name}}</li>
        @endforeach
         </ul>
        </div>
</body>

瀏覽下一看看app

還可使用

$loop->count 獲取總數
$loop->remaining 剩下幾個
$loop->first 是不是第一個
$loop->last 是不是最後一個

循環裏套循環
小循環裏也能夠用loop 用法:
$loop->partent->index 獲取父循環的id
 

分頁實現

例如 修改 routes/web.php

Route::get('/', function () {

   $users=\App\User::paginate(5);
   return view('welcome',compact('users'));
 
});

在對應的view頁面加上  $user->links()

<div class="flex-center position-ref full-height">
        <ul>
        @foreach($users as $user)
         <li>{{$loop->remaining}} {{$user->name}}</li>
        @endforeach
        {{$users->links()}}
        </ul>
  </div>

public/css目錄下自帶了 app.css樣式 這個樣式整合了bootstrap風格, 能夠加到view文件中

<link href="css/app.css" rel="stylesheet"  type="text/css" >

獲得漂亮的分頁效果

 

想自定義分頁效果怎麼辦? 你可使用命令 php artisan vendor:publish

這個命令能夠將站點中使用的vendor功能發佈到resources目錄

進入這個目錄resources/views/vendor/pagination果真看到生成了幾個文件
修改default.blade.php看看 是否會讓分頁部分有變化

保存之後看看效果 果真有變化 。

那若是我想啓用bootstrap-4.blade.php文件做爲分頁模版怎麼辦呢,只須要在頁面視圖文件resources/views/welcome.blade.php中的$users->links() 方法裏傳入參數便可 ,寫法爲 $users->links('vendor.pagination.bootstrap-4')  

<div class="flex-center position-ref full-height">
        <ul>
        @foreach($users as $user)
         <li>{{$loop->remaining}} {{$user->name}}</li>
        @endforeach
        {{$users->links('vendor.pagination.bootstrap-4')}}
         </ul>
</div>

而後修改一下resources/views/vendor/pagination/bootstrap-4.blade.php 看看分頁是否會有變化 .

 

使用 Mailable 發送郵件

建立email文件

使用命令 php artisan make:mail welcometo1ke

而後在 app/Mail/ 目錄下會生成文件 welcometo1ke.php

修改welcometo1ke.php文件的bulid方法

public function build()
    {
        return $this->view('email.welcome');
    }

而後建立對應的view文件 在resources/views目錄下建立email目錄 ,在resources/views/email目錄下建立welcome.blade.php文件

隨便在welcome.blade.php文件裏寫點東西 

而後修改路由文件routes/web.php

Route::get('/email',function(){
      Mail::to('lilugirl2005@126.com')->send(new \App\Mail\welcometo1ke());
});

 

修改config/mail.php文件中的from配置

'from' => [
        'address' => '794516454@qq.com',
        'name' => '10yuelive',
    ],

 

在.env文件中 MAIL_HOST對應的是mailtrap.io的服務 

註冊登陸mailtrap.io  ,mailtrap.io屏蔽了國內的一些郵箱,所以最好用gmail郵箱註冊

登陸後 點擊Demo inbox

複製用戶名密碼

相應的修改 .env文件中 對應的 MAIL_USERNAME和MAIL_PASSWORD

在瀏覽器裏輸入http://10yue.live/email 發送郵件

以後登陸到mailtrap 就能夠看到剛纔發送的郵件內容

這封郵件的內容就來自於以前編輯的郵件模版 resources/views/email/welcome.blade.php

下面演示如何在郵件中傳遞變量

app/Mail/welcometo1ke.php 文件改動以下

routes/web.php改動以下

resources/views/email/welcome.blade.php文件改動以下

從新在瀏覽器中打開 http://10yue.live/email 發送一次郵件

再到mailtrap查看發送結果

 

toggle功能

這裏經過用戶點擊收藏文章的場景展現toggle功能

這裏用戶和文章的收藏關係是多對多

先生成假數據 post 和收藏表 favorites

輸入命令

php artisan make:model Post -m
//生成Post model和migration文件
php artisan make:migration create_favorites_table --create=favorites
// 生成favorites表的migration文件

這時候會看到database/migration目錄下對應生成了兩個文件

修改...posts_table.php文件 在up方法中 添加兩個字段 title 和 body

修改...favorites_table.php文件 在up方法中增長兩個外健 user_id和post_id

修改數據生成文件 database/factories/ModelFacotry.php 增長Post表生成語句

而後就能夠執行命令 php artisan migrate 了

使用tinker命令生成30條post假數據

先執行

php artisan tinker

再輸入

factory(App\Post::class,30)->create()

user表以前已經造了n多假數據這裏就不造了

構建多對多關係

修改app/User.php文件 增長favorites方法

執行tinker命令測試 favorites方法

而後檢查數據庫 發現favorites表自動生成了一條記錄

相反的detach能夠刪除關係

favorites數據庫爲空

toggle的使用

 

Notify:Email 通知

郵件通知的應用場景:假設網站上每發佈一篇文章,咱們都email通知到用戶。
這個文章就是咱們上文定義的post表

建立通知文件

執行命令

php artisan make:notification PostPublished
//PostPublished是我本身取的名字 你能夠隨意取名

以後會在app/Notifications目錄下生成一個PostPublished.php文件

咱們先來發送一封默認的通知郵件

修改routes/web.php 添加一個路由 調用用戶的notify方法

用戶的notify方法之因此能夠調用是由於用戶的model文件app/User.php 中聲明瞭 use Notifiable;

在瀏覽器上執行 http://10yue.live/notify 果真看到通知郵件發出來了,在mailtrap裏查看

簡單自定義通知郵件內容

能夠經過修改app/Notifications/PostPublished.php 文件中的toMail方法 來改變郵件內容。

在瀏覽器上執行 http://10yue.live/notify 再次發送郵件 效果以下

修改config/app.php中的應用名稱 也能夠改變郵件相應內容

在郵件中注入變量

好比咱們把post信息注入

修改app/Notifications/PostPublished.php 文件


修改routes/web.php文件的郵件通知部分

發送郵件 查看發送內容

徹底定製化郵件模版

經過執行如下操做能夠徹底自由修改郵件模版

執行命令 

php artisan vendor:publish

而後在resources/views/vendor/notifications文件夾下就生成了email模版文件 

能夠經過修改email.blade.php文件完全修改email通知模版的內容

Notify:站內信通知 (經過database記錄)

站內信通知是將通知消息存儲到數據庫中的,須要先創建對應的數據表

輸入如下命令建立表

php artisan notifications:table

而後在database/migrations目錄生成了對應的migration文件

而後在命令行中輸入 php artisan migrate命令生成數據表

修改app/Notifications/PostPublished.php 文件中的配置

瀏覽器中執行http://10yue.live/notify  而後在數據庫notifications表中能夠看到一條記錄

添加另外一種通知類型 

好比說站內通知的類型還有 用戶訂閱成功的通知,那麼咱們就須要再新建一種通知類型

執行命令 

php artisan make:notification UserSubscribe

在 app/Notifications目錄生成了UserSubscribe.php文件

修改UserSubscribe.php文件以下

在routes/web.php上添加路由

在瀏覽器執行 http://10yue.live/subscribe

數據庫中果真多了一條記錄

綜合應用

下面咱們要根據以前學到的內容作一個簡單的消息通知頁面 

首先在routes/web.php文件中模擬某個用戶登陸 建立消息通知的路由 消息已讀路由等

在resources/views/notification目錄下建立 index.blade.php文件 內容以下

<h1>個人通知</h1>
<ul>
    @foreach(Auth::user()->unreadNotifications as $notification)
        @include('notifications.'.snake_case(class_basename($notification->type)))
    @endforeach
</ul>

<form method="POST" action="/mynote/read" accept-charset="UTF-8">
   {{csrf_field()}}
   {{method_field('DELETE')}}
   <button type="submit">標記已讀</button>
</form>

在resources/views/notification目錄下建立 user_subscribe.blade.php文件 內容以下

<li>
  {{$notification->data['subscribed_at']}}
</li>

在resources/views/notification目錄下建立 post_published.blade.php文件 內容以下

<h3>文章發佈通知</h3>
<li>{{$notification->data['title']}}</li>

 

在瀏覽器輸入 http://10yue.live/subscribe  和 http://10yue.live/notify

而後打開 https://10yue.live/mynote

就能看到以下內容

console Command 

routes/console.php 文件一瞥

在命令行執行命令

php artisan inspire

會獲得一個名言警句

嘗試在routes/console.php添加一個簡單的命令

Artisan::command('hello',function(){
    $this->comment('hey there');
});

執行命令  會獲得定義的字符串

php artisan hello

 

再嘗試一下帶參數的命令
routes/console.php添加代碼

Artisan::command('hello {name}',function(){

    $this->comment('hey '.$this->argument('name'));

});

實行命名

php artisan hello lilu

其中lilu是參數

 

給命令添加描述

查看命令描述或者解釋

php artisan
php artisan help hello

 

Passport 使用 OAuth 2.0 

相關文章
相關標籤/搜索