Things have changed for L5.2. You should put your routes inside the web middleware group.
Route::group(['middleware' => 'web'], function () {
// Put all routes here
});
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi there
Following the Laravel 5 Fundeamental tutorials I've stumbled upon an issue where the {{ var_dump($errors) }} code in my "create.blade.php" doesn't work, it hrows in this error:
https://www.dropbox.com/s/8m0ofiopuqsj06o/Screenshot%202016-03-02%2023.00.05.png?dl=0
I have tried this:
In your kernel.php file(app/Http/Kernel.php), you can move the middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class back to the protected $middleware property.
But then it throws in another error:
https://www.dropbox.com/s/kiupd1m7ppawthj/Screenshot%202016-03-02%2023.02.09.png?dl=0
Any ideas what might be wrong here? Thanks! :)
Things have changed for L5.2. You should put your routes inside the web middleware group.
Route::group(['middleware' => 'web'], function () {
// Put all routes here
});
You can also use {{ dd($errors) }}
Hmm, you mean like this?
Route::group(['middleware' => ['web']], function () {
Route::get('/', 'PagesController@welcome');
Route::get('contact', 'PagesController@contact');
Route::get('about', 'PagesController@about');
Route::get('articles', 'ArticlesController@index');
Route::get('articles/create', 'ArticlesController@create');
Route::get('articles/{id}', 'ArticlesController@show');
Route::post('articles', 'ArticlesController@store');
});
?
I'm getting another error then:
https://www.dropbox.com/s/0j5ki2skrx4kxpu/Screenshot%202016-03-02%2023.24.22.png?dl=0
@irmscher You are getting that error most likely because you changed your Kernel.php file. Move \Illuminate\View\Middleware\ShareErrorsFromSession::class back to where it was, which was inside the web middleware group.
tired it already but still getting an error that's saying:
Undefined variable: errors
isn't the $errors variable pre-defined by Laravel?
did that already but still an error:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
@irmscher Try posting both your Kernel file and routes file.
Kernel.php:
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];
}
routes.php:
<?php
/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
/*
Route::get('/', function () {
return view('welcome');
});
*/
Route::get('/', 'PagesController@welcome');
Route::get('contact', 'PagesController@contact');
Route::get('about', 'PagesController@about');
Route::get('articles', 'ArticlesController@index');
Route::get('articles/create', 'ArticlesController@create');
Route::get('articles/{id}', 'ArticlesController@show');
Route::post('articles', 'ArticlesController@store');
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web']], function () {
//
});
May be clear cache will help?
I have not touched the "middleware" thing cause I'm still confused...
@irmscher You shouldn't take your routes outside the web middleware. That is taking a step back. Try putting the routes back inside the web middleware. Kernel looks to be back to its original state so that looks okay. If you did cache your routes, then run php artisan route::clear.
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web']], function () {
Route::get('/', 'PagesController@welcome');
Route::get('contact', 'PagesController@contact');
Route::get('about', 'PagesController@about');
Route::get('articles', 'ArticlesController@index');
Route::get('articles/create', 'ArticlesController@create');
Route::get('articles/{id}', 'ArticlesController@show');
Route::post('articles', 'ArticlesController@store');
});
This should be your routes.php file. $errors is passed to Laravel by the Web Middleware.
Alright, I now figured out what the problem was,
an empty
Route::group(['middleware' => ['web']], function () {
});
was already preset in my routes.php, but it went below the "no-scroll range", probably because I was doing a lot of copy'n'paste stuff
So I was adding another one and that's what has been causing the error.
Thanks a lot guys, really much appreciated!
You can actually have multiple route web groups and be fine. :)
Route::group(['middleware' => ['web']], function () {
});
Route::group(['middleware' => ['web']], function () {
});
Route::group(['middleware' => ['web']], function () {
// Put routes here and it will still work
});
This isn't great code, but for sake of an example, it will still work. I'm guessing you needed both your Kernel and routes to be correct, but you kept only fixing one and reverting the other rather than correcting both.
Please or to participate in this conversation.