Route::Auth not surrounded in Middleware - php

<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for 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::auth();
Route::get('/home', 'HomeController#index');
Laravel routes.php. So when I look at Auth examples for Laravel 5.2, I ALWAYS ALWAYS see the Route::auth(); and Route::get('/home', 'HomeController#index') encapsulated in { of the Route::group(['middleware' => ['web']], function () { thing. But whenever I use the commands php artisan make:auth all it does is create the one I showed above.
Any clues on why this does this? It works fine and all but I'm not 100% sure if it's functioning properly. But I can tell you that I can login and sign-up properly. Did they do any changes to Laravel?

the web middleware is now applied by default:
https://github.com/laravel/laravel/tree/master/app/Http
Also in Kernel.php
https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php

Related

Target class [AdminController] does not exist

I have this error when a user returns from login to the admin page (i.e http://127.0.0.1:8000/admin), it should throw a 403 error if he/she doesn't sign in as the admin (i.e if he is not the admin)
Admin also experience this same error
Here's My Route
Laravel Version: 9.24.0
Please anyone should help
Here are my codes on web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
Route::middleware(['auth','admin'])->name('admin.')->prefix('admin')->group(function() {
Route::get('/', [AdminController::class, 'index'])->name('index');
});
require __DIR__.'/auth.php';
in app\Providers\RouteServiceProvider.php
uncomment this line
protected $namespace = 'App\\Http\\Controllers';
then command do
php artisan optimize
// or
php artisan optimize:clear
Thank you everyone for your help
I have identified the problem, I didn't add this on my kernel
'admin' => \App\Http\Middleware\Admin::class

How to create multiple route with laravel 5.8

I'm using version 5.8 of laravel and I'm trying to initialize the routes. It is not possible to access the route created after the default route:
Route::get('/', function () {
return view('welcome');
});
I try to add a parameter in the second route:
Route::get('/page', ['as' => 'home', function (/page) {return view('page1') ;}]);
I got 404 error. How can I use multiple routes ?
This is my web.php file:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/page', function() { return view('page1'); })->name('home');
Route::get('/', function () {
return view('welcome');
});
Did you copied the second route, or what are you trying to do with this?
You can try this one instead as your syntax is wrong:
Route::get('/page', function() { return view('page1'); })->name('home');
-- EDIT
You can also return just the views from your routes if you don't plan to use a controller, like this:
Route::view('/', 'welcome');
Route::view('/page', 'page1');

Unable to prepare route [api/user] for serialization. Uses Closure

I'm writing my project on Laravel. When I optimize the project, I have a problem :
Unable to prepare route [api/user] for serialization. Uses Closure.
I looked for any closures in web.php, but I didn't find anything
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/','ReviewsController#main')->name('main');
Route::post('/','MailController#verify')->name('verifyPost');
Route::get('/reviews', 'ReviewsController#index')->name('reviews');
Route::post('/reviews','ReviewsController#add')->name('addReview');
Auth::routes();
Route::group(['middleware' => 'admin','prefix' => 'admin'],function () {
Route::get('/', 'HomeController#index')->name('admin');
Route::get('/reviews', 'Admin\ReviewsController#get')->name('admin.reviews');
Route::get('/reviews/accepted/{id}','Admin\ReviewsController#accept')->where('id','\d+')->name('admin.accepted');
Route::delete('/reviews/delete','Admin\ReviewsController#delete')->name('reviews.delete');
});
in api.php file search and comment this route you will not get error..
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
and also in web.php file route::group is also closure and also comment them for test
Route::group(['middleware' => 'admin','prefix' => 'admin'],function () {
Route::get('/', 'HomeController#index')->name('admin');
Route::get('/reviews', 'Admin\ReviewsController#get')->name('admin.reviews');
Route::get('/reviews/accepted/{id}','Admin\ReviewsController#accept')->where('id','\d+')->name('admin.accepted');
Route::delete('/reviews/delete','Admin\ReviewsController#delete')->name('reviews.delete');
});
see what is closure
Php routing cache command :
php artisan route:cache
if your application using controller based routes. It help for fast execution. But remember "Closure based routes cannot be cached"
So kindly convert your Closure routes to controller classes.
For more information
Make sure to Check "routes/api.php"

New Laravel Routes not working

I have a problem, new routes in laravel are not working, url shows the correct route but almost as if it does not get to my routes web file just returns page not found every time.
I have tried:
using named route,
moving function to different controller,
clearing route cache,
clearing app cache,
dump-auto load,
made sure that AllowOverride is set to All,
Web.php:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
/*
|--------------------------------------------------------------------------
| Courses
|--------------------------------------------------------------------------
*/
Route::get('/courses', 'CourseController#index');
Route::get('/courses/create', 'CourseController#create');
Route::get('/courses/{course}', 'CourseController#show');
Route::get('/courses/{course}/edit', 'CourseController#edit');
Route::post('/courses', 'CourseController#store');
Route::patch('/courses/{course}', 'CourseController#update');
Route::delete('/courses/{course}', 'CourseController#destroy')->name('course-delete');
Route::get('/courses/statistics', 'CourseController#statistics');
/*
|--------------------------------------------------------------------------
| First Aid
|--------------------------------------------------------------------------
*/
Route::get('/section/{section}', 'SectionController#show');
/*
|--------------------------------------------------------------------------
| First Aid
|--------------------------------------------------------------------------
*/
Route::get('/progress', 'UserProgressController#index');
Route::get('/progress/create', 'UserProgressController#create');
Route::get('/progress/{section}', 'UserProgressController#show');
Route::get('/progress/formativeresults', 'UserProgressController#formativeresults');
//Route::get('/progress/coursestatistics', 'UserProgressController#coursestatistics');
//Route::get('/progress/{progress}/edit', 'UserProgressController#edit');
Route::post('/progress', 'UserProgressController#store');
//Route::patch('/progress/{progress}', 'UserProgressController#update');
//Route::delete('/progress/{progress}', 'UserProgressController#destroy')->name('progress-delete');
Controller:
public function statistics()
{
dd('Test');
return view('coursestatistics');
}
View file name:
coursestatistics.blade.php file structure views/coursestatistics
Link to page:
<a class="navbar-brand" href="/courses/statistics">
{{ __('Statistics') }}
</a>
Can anyone tell me what might be causing route not to work?
Try placing
Route::get('/courses/statistics', 'CourseController#statistics');
below this particular line of route code
Route::get('/courses/create', 'CourseController#create');
The general rule of laravel routing is to place specific routes before wildcard routes that are related. Link here
I had same issue, Did all those magic with configs and nothing..
Solution: run: php artisan route:clear
If issue remains same After cache clear or laravel routing rule, use 'composer dump-autoload'
If in your controller you have Model::findorFail($id) and an object with that ID does not exist, it can also lead to this error (in Laravel 6)

Why i can not register and login in Laravel 5.2?

I have faced a problem with Laravel 5.2 login and register.I used here Laravel 5.2 default login.blade.php and register.blade.php.All things going well but when i trying to register any user and fill up the form and submit then it do not insert any data in database and same page show in the browser window.Browser did not showed any error though i have made debug true.
Here is my routes.php:
<?php
use App\Member;
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| 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('/home', function () {
return view('home');
});
Route::get('/members', 'MemberController#index');
Route::post('/member', 'MemberController#store');
Route::delete('/member/{member}', 'MemberController#destroy');
// Authentication Routes...
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#logout');
// Registration Routes...
Route::get('auth/register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
/*
|--------------------------------------------------------------------------
| 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::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController#index');
});
I have read many question in Stackoverflow.But all are failed to solve my problem.Here is some link of those questions:
Can not login and register in laravel 5.1
Laravel 5.2 /login & /register not working
Laravel 5.2 Authentication - How can I show logged in user's name and the logout link in every page?
For find solution if you need any files then please let me know.Then i will provide here.
You've read the answer, you're just not applying it correctly. Auth requires sessions. Any route that needs session information should be inside the web middleware group.
Right now, your member, members, auth/login, auth/logout, auth/register, 'home', and / routes are all outside of the web middleware group, so none of them will have session information available (meaning none of them will show users as logged in).

Categories