2017-12-22 13 views
0

ビューファイルで使用できるlaravelとauth(ゲート、ミドルウェア)間の唯一の認可がわかっているので、ビューファイルの一部のコンテンツ制限にユーザーの承認を加えようとしています。 (policy)authです。 私のデータソースはモデルではありませんが、私はちょうどいくつかのWebサービスからデータを取得していますが、ポリシー認証はlaravelモデルを使用して動作しています。アプリの\のWebUserはそれがWebサービスからユーザー情報を取得するだけのクラスですが、モデルファイルではありません。laravelポリシーを使用するブレードビューファイルでの承認

私は新しいポリシー(isPlatinum)

<?php 

namespace App\Policies; 

use App\User; 
use App\WebUser; 
use Illuminate\Auth\Access\HandlesAuthorization; 

class isPlatinum 
{ 
    use HandlesAuthorization; 

    /** 
    * Create a new policy instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
    } 

    public function view(WebUser $user){ 
     return $user->user->subscription->type === 'platinum'; 
    } 

} 

お知らせを作成しました。私のブレードビューファイル与える

@cannot('view',$isPlatinum) 
     This page is for Platinum Users 
     @endcannot 

エラーで

Undefined variable: isPlatinum (View: 

私は私は私の状況では、唯一のモデルのためにLaravelポリシー認証を使用することになっていることを知っている、右laravelの認証Iものです使用する必要があり、私は使用することができます内部のビューファイルですか?

答えて

0

isPlatinum変数を表示することができませんでした。あなたはshare data with all views次のことができます。以下のように

は、サービスプロバイダを作成します。 { "エラー:

<?php 

namespace App\Providers; 

use Illuminate\Support\Facades\View; 

class AuthServiceProvider extends ServiceProvider 
{ 
    /** 
    * Bootstrap any application services. 
    * 
    * @return void 
    */ 
    public function boot() 
    { 
     // method to fetch the webUser? 
     // $webUser = ; 
     View::share('isPlatinum', $webUser->user->subscription->type === 'platinum'); 
    } 

    /** 
    * Register the service provider. 
    * 
    * @return void 
    */ 
    public function register() 
    { 
     // 
    } 
} 
+0

おかげで、これは私が私のために最良の選択肢だと思う方法です、私は応答を示すあなたが言及した、まさに、エラーをしました":" token_not_provided "}、トークンはApp \ WebUserのWebサービスリクエストに提供され、トークンの値はSession :: get( 'token')、dd(Session :: get( 'token'))はトークンを返す何の問題もなく、私はちょうどAuthServiceProviderがWebUser内のセッション値で動作していないのだろうか? – user2873860

+0

https://laracasts.com/discuss/channels/general-discussion/laravel-5-session-data-is-not-accessible-in-the-app-boot-processを参照してください。プロバイダのブート方法は、すべてのプロバイダが登録され、セッションが機能するはずです。私の場合、WebUserトークンの値はセッション – user2873860

+0

https://stackoverflow.com/questions/34577946/how-to-retrieve-session-data-in-service-providers-in-laravel – Ben

関連する問題