-1

I am trying to do a before-action check on a route. Therefore I set up a middleware as follows:

<?php

namespace App\Http\Middleware;

use Closure;
use Route;

class PermissionMiddleware
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{
   list(, $action) = explode('@', Route::getCurrentRoute()->getActionName());

    return $next($request);
}
}

In my controller, I do as follows:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Blog;
use Image;
use File;
use Route;

class BlogController extends Controller
{

public function __construct()
{
    $this->middleware(['auth','permission']);
}


public function create()
{       
   .......
   .......
}

And my route is as follows:

Route::get('create/news',"BlogController@create");

However, when I navigate to the route, it displays an error as follows:

enter image description here

Giving an exception as follows:

FatalErrorException Call to a member function getActionName() on null in PermissionMiddleware.php (line 19)

I have tried to no avail to resolve this.

Josh
  • 1,660
  • 5
  • 33
  • 55
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jul 04 '17 at 07:55

1 Answers1

0

Read again the error message and you'll find the problem.

The error is:

FatalErrorException Call to a member function getActionName() on null in PermissionMiddleware.php (line 19)

On line 19, I guess you have this:

list(, $action) = explode('@', Route::getCurrentRoute()->getActionName());

That means that error comes from here: "Route::getCurrentRoute()->getActionName()".

Route::getCurrentRoute() returns null. So you call the method "getActionName()" from null.

Please check the method "getCurrentRoute()" in order to fix the problem. Good luck.

Pascut
  • 3,291
  • 6
  • 36
  • 64
  • Yes. You are right. Route::getCurrentRoute() returns null and I don't know the reason for that. Any clue to why that could happen? – Josh Jul 04 '17 at 09:38
  • Thank you but I actually saw that before. But what I am looking for is the routeAction() and not the routeName() and that link you referred me to is discussing the routeName() rather – Josh Jul 04 '17 at 11:28