1
Laravelの新機能です。現在私はlaravel authを使っています(php artisan make:auth)。モデルをLaravel 5.2のカスタムミドルウェアファイルに呼び出す関数
ログインしたユーザーのデータを「管理者」からカスタムミドルウェアにプルする必要がありますが、モデルで定義されている「管理」ファイルで定義されている機能を呼び出すことができません。
モデルファイル: - アプリ\ Admin.php
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function roles()
{
return $this->belongsToMany('App\Role', 'admin_role', 'admin_id', 'role_id');
}
public function hasAnyRole($roles)
{
if (is_array($roles)) {
foreach ($roles as $role) {
if ($this->hasRole($role)) {
return true;
}
}
} else {
if ($this->hasRole($roles)) {
return true;
}
}
return false;
}
public function hasRole($role)
{
if ($this->roles()->where('name', $role)->first()) {
return true;
}
return false;
}
public function myfunction($val)
{
echo "===>".$val; exit ;
}
}
ミドルウェアFILE: - アプリ\のHttp \ミドルウェア\ CheckRole.php
<?php
namespace App\Http\Middleware;
use Closure;
class CheckRole
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$request->admin->myfunction('customvalue');
exit ;
}
}
私はすなわち "MyFunctionを" 機能を呼び出す必要があります管理モデルでCheckrole.phpミドルウェアに定義されています。
おかげ
こんにちは、応答のおかげで、残念ながらそれは動作しません。私は「非オブジェクト上のメンバ関数myfunction()を呼び出す」というエラーが発生していて、私の質問を編集しました。もう一度レビューできますか? – Abhishek
あなたはユーザーとして認証されていますか? dd(auth() - > check())とは何ですか?ミドルウェアでやる? – pseudoanime
これは、ユーザがログインしていないことを意味する "false" – Abhishek