Laravel中间件

时间:2016-06-06 12:09:49

标签: laravel laravel-5

我想做类似这样的事情,以下是代码示例:

public function handle()
{
    $user = Auth::user()->name;
    $ABC = ABC::find($id);
    $author = $ABC->author;
    if(!$user == $author){
        echo "He can comment";
    }
    else{
         //delete comment 
    }

    return redirect()->back();

}

只有帖子的所有者可以添加评论我想用中间件来做,有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:3)

例如,

<?php
  namespace Lame\Http\Middleware\Comment;

  class CanAddComment
  {

      public function handle($request, Closure $next)
      {
          $user = Auth::user()->name;
          $ABC = ABC::find($id);
          $author = $ABC->author;
          if($user == $author){
              return redirect()->back();
          }

          return $next($request);
      }
}