I can list all my route in console, but when I add this on my route.php, I've got fatal error
Route::group(['middleware' => 'is_admin', 'prefix' => 'admin', 'as'=>'admin.'], function () {
Route::get('/', ['as'=>'dashboard', 'uses'=>'AdminController@dashboard']);
Route::resource('questions','QuestionController');
});
and this is on my Middleware/IsAdmin.php
namespace App\Http\Middleware;
use Closure;
class IsAdmin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(auth()->user()->isAdmin()) {
return $next($request);
}
return redirect('home');
}
}
Update 2 Even I only got this, it still error.
Route::group(['middleware' => 'is_admin', 'prefix' => 'admin', 'as'=>'admin.'], function () {
Route::get('/', ['as'=>'dashboard', 'uses'=>'AdminController@dashboard']);
});
Update 3 - AdminController
...
class AdminController extends Controller
{
private $page_name;
public function __construct()
{
$this->middleware('auth');
//when I comment this below, it works.
$this->page_name = ucfirst(substr(\Request::route()->getName(), strpos(\Request::route()->getName(), "/") + 1));
}
...
The problem here is that you have:
$this->page_name = ucfirst(substr(\Request::route()->getName(), strpos(\Request::route()->getName(), "/") + 1));
in controller constructor (you already noticed).
When you are using console, you don't have any route so it won't work.
If you really need this line you can wrap this with additional condition:
if (!app()->runningInConsole())
{
$this->page_name = ucfirst(substr(\Request::route()->getName(), strpos(\Request::route()->getName(), "/") + 1));
}
but you can also extract this line into separate method and use it in other methods when needed.