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"
Related
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
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');
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)
I am attempting to navigate to a product page with laravel. The URL gets an id which is obtained from the database id which it uses to display the product.
Products controller file:
public function index()
{
$products = Product::all();
return view('products.index')->with('products',$products);
}
public function show($id)
{
$product = Product::find($id);
return view('products.show')->with('product',$product);
}
Error:
The requested URL /products/1 was not found on this server.
Here's my routes as requested:
<?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('HomePage');
});
Route::get('/about', function () {
return view('pages.about');
});
/*
* Login
*/
Route::get('/login', [
'as' => 'login',
'uses' => 'UserLoginController#showLogin',
]);
Route::post('/login', 'UserLoginController#postLogin');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('products','productcontroller');
You don't have any route like this /products/1 so need to add that route and assign the controller and action that you need . Eg.
Route::get('/product/{id}', 'ProductController#show')
Edit: is your controller class named correctly? I think issue is there. Yes you can use resource route for restful request response but route can't find your controller class. Name it correctly. ProductsController or something. Double check.
Can you paste your route? Problem is probably there. Not in your controller. Check the documentation, if you can't figure it out paste here.
the href tag was wrong and needed to be exactly what was in the localhost e.g. localhost/8888/public
<?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