简体   繁体   English

中间件对于控制器中的操作,Laravel 5.1

[英]Middleware For actions into Controller, Laravel 5.1

I have a Controller that have a index method, but also have a multiple levels of users that have access to this method, but single the admin can view all records of the database How can i add a middleware for select the action corresponding to user? 我有一个具有索引方法的Controller,但也具有可以访问此方法的多个级别的用户,但是一个管理员可以查看数据库的所有记录。如何添加中间件来选择与用户相对应的操作? I have the next code 我有下一个代码

<?php

namespace SET\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Redirect;
use SET\Http\Requests;
use SET\Http\Requests\LabRequest;
use SET\Http\Controllers\Controller;
use Illuminate\Routing\Route;
use SET\Lab;
use Validator;
use Auth;
use DB;

class LaboratorioController extends Controller
{
public function __construct(){
    $this->beforeFilter('@find', ['only' => ['show','edit','update','destroy']]);
}

public function find(Route $route){
    $this->laboratorio = Lab::findOrFail($route->getParameter('laboratorio'));
}
/**
 * Display a listing of the resource.
 *
 * @return Response
 */

public function index()
{
    $labs = Lab::all();
    return view('comp.lab.index',['labs' => $labs]);
}

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    return view('comp.lab.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store(LabRequest $request)
{       Lab::create($request->all());
        Session::flash('message','Laboratorio creado correctamente');
        return Redirect::to('/laboratorio');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show()
{
     $teams = $this->laboratorio;
    return view('comp.lab.show',['lab'=>$this->laboratorio]);
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit()
{
    return view('comp.lab.edit',['lab'=>$this->laboratorio]);
}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update(LabRequest $request)
{
    $this->laboratorio->update($request->all());
    Session::flash('message','El laboratorio se actualizo correctamente');
    return Redirect::to('/laboratorio');
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy()
{
    $this->laboratorio->delete();
    Session::flash('message','El laboratorio fue eliminado correctamente');
    return Redirect::to('/laboratorio');
}

Thanks :D 感谢:D

Here's a general idea: 这是一个总体思路:

$user_group = $user::get_user_group($user_id)
if($user_group->group_id === 'something'){
    \\method to return data for group 'something'
}

Get data by groups 按组获取数据

$user_data = $user::get_data_by_group_id($group_id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM