1郵件 2隊列 3redis 4cache 5基礎控制器 6定時任務 7policy
smtp.163.com
25
gujinrong1234
1郵件
2隊列
3redis
4cache
5基礎控制器
6定時任務
7policy
https://www.lijinma.com/blog/2016/12/14/laravel-policy/
http://laravelacademy.org/post/6823.html
1
$doc =sotrge_path(‘app/aa.doc’);位置在sotrge/app/aa.doc
$url = ‘’
$img = Sotrage::get(‘1.jpg’) //位置在sotrge/app/1.jpg
Mail::send(‘welcome’,[‘aa’=>$aa],function($message){
$message->form(‘[email protected]’,’gu’)->to(‘18552368312’)->subject(‘主題’)
$message->attach($doc,[‘as’=>’附件.doc’]);
})
在視圖裏面
<img src =“{{$message->embed($url)}}”>
<img src =“{{$message->embedData($img,111.jpg)}}”>
MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=25
MAIL_PASSWORD=gujinrong1234
MAIL_ENCRYPTION=null
Choore Application
2
Cache::put(1,2,11);
echo Cache::get(1);
Cache::forget()
Cache::has()
Session::put(‘s1‘,11);
echo Session::get(‘s11‘,11);
//檢測是否存在key
Session::has(‘users‘);
//刪除key
Session::forget(‘key‘);
//保存key,value
Session::flash(‘key‘, ‘value‘);
//取值方法還是一樣的
Session::get(‘key‘);
Session::all()
Cookie
存
return response(‘歡迎來到 Laravel 學院‘)->cookie(
‘name‘, ‘學院君‘, $minutes, $path, $domain, $secure, $httpOnly
);
如果你想要生成一個 Symfony\Component\HttpFoundation\Cookie 實例以便後續添加到響應實例,可以使用全局輔助函數 cookie,該 Cookie 只有在添加到響應實例上才會發送到客戶端:
$cookie =Cookie::make(‘name1‘,‘gujinrong‘,11);
$cookie = cookie(‘name‘, ‘學院君‘, $minutes);
return response(‘歡迎來到 Laravel 學院‘)->cookie($cookie);
取
為了安全起見,Laravel 框架創建的所有 Cookie 都經過加密並使用一個認證碼進行簽名
$value = $request->cookie(‘name‘); //取
echoCookie::get(‘name1‘);
權限Gates
1
Gates 是一個用於判斷用戶是否有權進行某項操作的閉包,通常使用Gate 門面定義在 App\Providers\AuthServiceProvider類中
public function boot()
{
$this->registerPolicies();
Gate::define(‘update-post‘, function ($user, $post) {
return $user->id == $post->user_id;
});
}
if (Gate::allows(‘update-post‘, $post)) {
// 當前用戶可以更新文章...
}
if (Gate::denies(‘update-post‘, $post)) {
// 當前用戶不能更新文章...
}
如果你想要判斷指定用戶(非當前用戶)是否有權進行某項操作,可以使用 Gate 門面上的 forUser 方法:
if (Gate::forUser($user)->allows(‘update-post‘, $post)) {
// 當前用戶可以更新文章...
}
if (Gate::forUser($user)->denies(‘update-post‘, $post)) {
// 當前用戶不能更新文章...
}
權限2
Policies(策略)是用於組織基於特定模型或資源的授權邏輯的類,例如,如果你開發的是一個博客應用,可以有一個 Post 模型和與之對應的 PostPolicy 來授權用戶創建或更新博客的動作。
步驟1
php artisan make:policy DocPolicy
Php artisan make:policy DocPolicy -m Docs
步驟2
註冊策略類
Laravel 自帶的AuthServiceProvider 包含了一個 policies 屬性來映射Eloquent 模型及與之對應的策略類。註冊策略將會告知 Laravel 在授權給定模型動作時使用哪一個策略類
protected $policies = [
‘App\Model‘ => ‘App\Policies\ModelPolicy‘,
Docs::class=>DocPolicy::class
];
步驟3
public function update(User $user, Post $post)
{
return $user->id === $post->user_id;
}
public function delete(User $user, Docs $docs)
{
//
}
public function create(User $user)
{
//
}
public function view(User $user, Docs $docs)
{
//
}
特殊
策略過濾器
對特定用戶,你可能想要在一個策略方法中對其授權所有權限,比如後臺管理員。要實現這個功能,需要在策略類中定義一個 before 方法,before 方法會在策略類的所有其他方法執行前執行,從而確保在其他策略方法調用前執行其中的邏輯:
public function before($user, $ability)
{
if ($user->isSuperAdmin()) {
return true;
}
}
如果你想要禁止所有授權,可以在 before 方法中返回 false。如果返回 null,該授權會落入策略方法。
步驟4使用1
Laravel 自帶的 User 模型提供了兩個方法用於授權動作:can 和 cant。can 方法接收你想要授權的動作和對應的模型作為參數。例如,下面的例子我們判斷用戶是否被授權更新給定的 Post 模型
if ($user->can(‘update‘, $post)) {
//
}
如果給定模型對應的策略已經註冊,則 can 方法會自動調用相應的策略並返回布爾結果。如果給定模型沒有任何策略被註冊,can 方法將會嘗試調用與動作名稱相匹配的Gate閉包。
create不需要傳$post,傳Post::class就可以了
有些動作比如 create 並不需要依賴給定模型實例,在這些場景中,可以傳遞一個類名到 can 方法,這個類名會在進行授權的時候用於判斷使用哪一個策略:
use App\Post;
if ($user->can(‘create‘, Post::class)) {
// Executes the "create" method on the relevant policy...
}
使用2
通過控制器輔助函數
除了提供給 User 模型的輔助函數,Laravel 還為繼承自 App\Http\Controllers\Controller 基類的所有控制器提供了 authorize 方法,和can 方法類似,該方法接收你想要授權的動作名稱以及相應模型實例作為參數,如果動作沒有被授權, authorize 方法將會拋出 Illuminate\Auth\Access\AuthorizationException ,Laravel 默認異常處理器將會將其轉化為狀態碼為 403 的 HTTP 響應:
public function update(Request $request, Post $post)
{
$this->authorize(‘update‘, $post);
// The current user can update the blog post...
}
public function create(Request $request)
{
$this->authorize(‘create‘, Post::class);
// The current user can create blog posts...
}
使用3在blade模板裏面
通過Blade模板
編寫 Blade 模板的時候,你可能想要在用戶被授權特定動作的情況下才顯示對應的視圖模板部分,例如,你可能想要在用戶被授權更新權限的情況下才顯示更新表單。在這種情況下,你可以使用 @can 和 @cannot 指令:
@can(‘update‘, $post)
<!-- 當前用戶可以更新文章 -->
@elsecan(‘create‘, $post)
<!-- 當前用戶可以創建新文章 -->
@endcan
@cannot(‘update‘, $post)
<!-- 當前用戶不能更新文章 -->
@elsecannot(‘create‘, $post)
<!-- 當前用戶不能創建新文章 -->
@endcannot
這種寫法可看作是 @if 和@unless 語句的縮寫,上面的 @can 和 @cannot 語句與下面的語句等價:
@if (Auth::user()->can(‘update‘, $post))
<!-- 當前用戶可以更新文章 -->
@endif
@unless (Auth::user()->can(‘update‘, $post))
<!-- 當前用戶不能更新文章 -->
@endunless
不依賴模型的動作
和其它授權方法一樣,如果授權動作不需要傳入模型實例的情況下可以傳遞類名給 @can 和 @cannot 指令:
@can(‘create‘, Post::class)
<!-- 當前用戶可以創建文章 -->
@endcan
@cannot(‘create‘, Post::class)
<!-- 當前用戶不能創建文章 -->
@endcannot
1郵件 2隊列 3redis 4cache 5基礎控制器 6定時任務 7policy