扩展 Laravel 基础控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40661705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extending Laravel base controller
提问by Josh
I am a newbie in Laravel framework and I want to extend a base controller which in turn extends controller. However, I discovered that when I do that, my controller no longer recognises my session variables.
我是 Laravel 框架的新手,我想扩展一个基本控制器,然后扩展控制器。但是,我发现当我这样做时,我的控制器不再识别我的会话变量。
Here is my code
这是我的代码
namespace App\Http\Controllers\Settings;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Auth\PermissionController;
use App\Fee;
class FeeController extends PermissionController
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
dd(session('userdata')['user_urls']);
$data['title']="Fees";
$data['fees']=Fee::all();
return view('settings.fee.index',$data);
}
And this is my PermissionController code
这是我的 PermissionController 代码
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class PermissionController extends Controller {
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct(Request $request) {
if(!session('userdata')['user_urls']->contains($request->path())){
dd(session('userdata')['user_urls']);
}
}
}
But I realize that my session('userdata')['user_urls']
becomes null at the PermissionController. But if I make FeeController to extend Controller, my session variables are intact.
但我意识到我session('userdata')['user_urls']
在 PermissionController 处变为空。但是如果我让 FeeController 扩展 Controller,我的会话变量是完整的。
I need to use the session variables for some control at the permission controller.
我需要在权限控制器上使用会话变量进行一些控制。
I am running Laravel 5.3 on a MAC OSX and PHP 7
我在 MAC OSX 和 PHP 7 上运行 Laravel 5.3
采纳答案by Josh
I have solved the same problem with middleware. I have created a middleware that takes care of the authorization of requests by checking the session to ensure that the controller action being accessed is available in session.
我已经用中间件解决了同样的问题。我创建了一个中间件,它通过检查会话来处理请求的授权,以确保正在访问的控制器操作在会话中可用。
This is the middleware
这是中间件
namespace App\Http\Middleware;
use Closure;
class PermissionMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$currentAction = \Route::currentRouteAction();
list($nothing,$route_action) = explode('App\Http\Controllers\', $currentAction);
$user_actions=session('userdata')['user_urls'];
if((empty($user_actions))||!$user_actions->contains($route_action)){
return redirect('denied');
}
return $next($request);
}
}
This is the controller
这是控制器
namespace App\Http\Controllers\Settings;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Auth\PermissionController;
use App\Fee;
class FeeController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$data['title']="Fees";
$data['fees']=Fee::all();
return view('settings.fee.index',$data);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(Request $request)
{
$data['title']='New Fee';
return view('settings.fee.create',$data);
}
So, instead of using the routes (cos of some other reasons), I used the controller actions. So, once a user logs in, all the controller actions he can access are loaded into session. When he tries to perform any action, the middleware, does the check to ensure he is allowed to perform that action. Otherwise, he is routed away.
因此,我没有使用路由(由于其他一些原因),而是使用了控制器操作。因此,一旦用户登录,他可以访问的所有控制器操作都会加载到会话中。当他尝试执行任何操作时,中间件会进行检查以确保他被允许执行该操作。否则,他会被赶走。
So, I either add 'permission' to the routes middleware or call
所以,我要么向路由中间件添加“权限”,要么调用
$this->middleware('permission')
$this->middleware('permission')
on the controller's construct method.
关于控制器的构造方法。
That is working for me now.
那现在对我有用。
Thank you everybody for your contributions.
感谢大家的贡献。
回答by PassionInfinite
I have figured out the problem. Actually, PermissionController is not registered in the web middleware group so that session is not persisting in the PermissionController. So the solution to your question is just make a trait named as Permission instead of the controller and use it in FeesContorller.
我已经想通了这个问题。实际上,PermissionController 未在 Web 中间件组中注册,因此会话不会保留在 PermissionController 中。因此,您的问题的解决方案只是创建一个名为 Permission 而不是控制器的特征,并在 FeesContorller 中使用它。
trait Permission{
public function permission(Request $request) {
if($request->session()->get('name') != null){
echo "Hello World";
}
}
}
And FeesController like this:
和 FeesController 像这样:
class FeesController extends Controller
{
use Permission;
public function index(Request $request)
{
$this->permission($request); // the method of the trait.
echo "\n".$request->session()->get('name');
}
}
Output:
输出:
If the name attribute is set in session then :
如果在会话中设置了 name 属性,则:
Hello World
Passion Infinite
Otherwise
除此以外
No Output // null