Laravel - Error "Unable to prepare route [/] for serialization. Uses Closure." - php
I'm new to Laravel and now i manage project that was left by someone.
i try to add a function to API, what i edit :
1) Add a Method :
myproject/app/Http/Controllers/Api/ArticleController.php
2) Add Routes to controler :
myprojectmyproject/routes/api.php
However, when i try to run php artisan route:cache i got below error :
Route cache cleared! \n
LogicException : Unable to prepare route [/] for serialization. Uses Closure.
my route file, myproject/routes/api.php :
Route::group (['prefix' => 'v1', 'middleware' => 'ValidateHeaderSignature'], function() {
Route::group(['prefix' => 'auth'], function() {
Route::post('/login', 'Api\AuthController#login');
Route::post('/register', 'Api\AuthController#register');
Route::post('/login-social-media', 'Api\AuthController#loginSocialMedia');
Route::post('/forgot-password', 'Api\AuthController#forgotPassword');
Route::group(['middleware' => 'jwt.auth'], function() {
Route::patch('/change-password', 'Api\AuthController#changePassword');
Route::post('/logout', 'Api\AuthController#logout');
});
});
Route::group(['prefix' => 'foundation-donate'], function() {
Route::get('/', 'Api\FoundationDonateController#index');
});
Route::group(['prefix' => 'greeting-chat'], function() {
Route::get('/', 'Api\GreetingChatController#index');
});
Route::group(['prefix' => 'prayer-time'], function () {
Route::get('/', 'Api\PrayerTimeController#index');
Route::get('/montly', 'Api\PrayerTimeController#getMontlyPrayerTimes');
});
Route::group(['prefix' => 'asmaul-husna'], function () {
Route::get('/', 'Api\AsmaulHusnaController#index');
});
Route::group(['prefix' => 'guidance'], function () {
Route::get('/zikir', 'Api\GuidanceController#zikirGuidances');
Route::get('/prayer', 'Api\GuidanceController#prayerGuidances');
});
Route::group(['prefix' => 'duas'], function () {
Route::get('/', 'Api\DuasController#index');
Route::get('/index', 'Api\DuasController#index');
Route::get('/all', 'Api\DuasController#allPrayers');
Route::get('/category/{category}', 'Api\DuasController#category');
Route::get('/show/{id}', 'Api\DuasController#show');
});
Route::group(['prefix' => 'zakat'], function () {
Route::get('/', 'Api\ZakatController#index');
Route::get('/index', 'Api\ZakatController#index');
Route::get('/all', 'Api\ZakatController#allPrayers');
Route::get('/category/{category}', 'Api\ZakatController#category');
Route::get('/show/{id}', 'Api\ZakatController#show');
});
Route::group(['prefix' => 'playlist'], function () {
Route::get('/zikir', 'Api\PlaylistSongController#playlistZikir');
Route::get('/shalawat', 'Api\PlaylistSongController#playlistShalawat');
Route::get('/duas', 'Api\PlaylistSongController#playlistDuas');
Route::get('/murottal', 'Api\PlaylistSongController#playlistMurottal');
Route::get('/songs', 'Api\PlaylistSongController#playlistSongs');
});
Route::group(['prefix' => 'dzikir'], function() {
Route::get('/primary', 'Api\DzikirController#primaryDzikir');
Route::get('/my-dzikir', 'Api\DzikirController#myDzikir');
Route::get('/categories', 'Api\DzikirController#dzikirCategories');
Route::group(['middleware' => 'jwt.auth'], function() {
Route::get('/point-total', 'Api\DzikirController#pointTotal');
Route::get('/histories', 'Api\DzikirController#histories');
Route::get('/total-dzikir-history', 'Api\DzikirController#totalDzikirHistory');
Route::post('/post-dzikir', 'Api\DzikirController#postDzikir');
});
});
Route::group(['prefix' => 'sadaqah'], function() {
Route::group(['middleware' => 'jwt.auth'], function() {
Route::get('/histories', 'Api\DzikirController#sadaqahHistories');
});
});
Route::group(['prefix' => 'article'], function() {
Route::get('/', 'Api\ArticleController#index');
Route::get('/daily-reflection', 'Api\ArticleController#getDailyReflection');
Route::get('/get-random', 'Api\ArticleController#getRandom');
});
Route::group(['prefix' => 'notification'], function() {
Route::get('/quote', 'Api\NotificationController#prayerQuotes');
});
Route::group(['prefix' => 'user', 'middleware' => 'jwt.auth'], function() {
Route::get('/show', 'Api\UserController#show');
Route::patch('/update-profile', 'Api\UserController#update');
});
Route::group(['prefix' => 'master'], function() {
Route::get('/location', 'Api\MasterController#location');
});
});
if i got error because of Uses Closure, why previous developer can populate route?
by run php artisan route:list i can see list of route that have ever made before.
any idea ?
===Update, Add routes/web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes(['register' => false]);
Route::get('/home', 'HomeController#index')->name('home');
Route::get('register/activation/{code}', 'Auth\\RegisterController#activation')->name('register.activation');
Route::group(['prefix' => 'admin', 'middleware' => ['auth']], function() {
Route::get('/user-apps/list-index', ['as' => 'user-apps.list-index', 'uses' => 'Admin\\UserAppsController#listIndex']);
Route::get('/user-apps/resend-confirmation', ['as' => 'user-apps.resend-confirmation', 'uses' => 'Admin\\UserAppsController#resendConfirmation']);
Route::resource('/user-apps', 'Admin\\UserAppsController');
Route::get('/user/list-index', ['as' => 'user.list-index', 'uses' => 'Admin\\UserController#listIndex']);
Route::resource('/user', 'Admin\\UserController');
Route::get('/dzikir-playlist-category/list-index', ['as' => 'dzikir-playlist-category.list-index', 'uses' => 'Admin\\DzikirPlaylistCategoryController#listIndex']);
Route::resource('/dzikir-playlist-category', 'Admin\\DzikirPlaylistCategoryController');
Route::get('/dzikir-playlist/list-index', ['as' => 'dzikir-playlist.list-index', 'uses' => 'Admin\\DzikirPlaylistController#listIndex']);
Route::resource('/dzikir-playlist', 'Admin\\DzikirPlaylistController');
Route::get('/dzikir-playlist-homepage/list-index', ['as' => 'dzikir-playlist-homepage.list-index', 'uses' => 'Admin\\DzikirPlaylistHomepageController#listIndex']);
Route::resource('/dzikir-playlist-homepage', 'Admin\\DzikirPlaylistHomepageController');
Route::get('/dzikir-playlist-my-zikir/list-index', ['as' => 'dzikir-playlist-my-zikir.list-index', 'uses' => 'Admin\\DzikirPlaylistMyZikirController#listIndex']);
Route::resource('/dzikir-playlist-my-zikir', 'Admin\\DzikirPlaylistMyZikirController');
Route::get('/greeting-chat/list-index', ['as' => 'greeting-chat.list-index', 'uses' => 'Admin\\GreetingChatController#listIndex']);
Route::resource('/greeting-chat', 'Admin\\GreetingChatController');
Route::get('/foundation-donate/list-index', ['as' => 'foundation-donate.list-index', 'uses' => 'Admin\\FoundationDonateController#listIndex']);
Route::resource('/foundation-donate', 'Admin\\FoundationDonateController');
Route::get('/asmaul-husna/list-index', ['as' => 'asmaul-husna.list-index', 'uses' => 'Admin\\AsmaulHusnaController#listIndex']);
Route::resource('/asmaul-husna', 'Admin\\AsmaulHusnaController');
Route::get('/guidance/list-index', ['as' => 'guidance.list-index', 'uses' => 'Admin\\GuidanceController#listIndex']);
Route::resource('/guidance', 'Admin\\GuidanceController');
Route::get('/content-category/list-index', ['as' => 'content-category.list-index', 'uses' => 'Admin\\ContentCategoryController#listIndex']);
Route::resource('/content-category', 'Admin\\ContentCategoryController');
Route::get('/duas/list-index', ['as' => 'duas.list-index', 'uses' => 'Admin\\DuasController#listIndex']);
Route::resource('/duas', 'Admin\\DuasController');
Route::get('/zakat/list-index', ['as' => 'zakat.list-index', 'uses' => 'Admin\\ZakatController#listIndex']);
Route::resource('/zakat', 'Admin\\ZakatController');
Route::get('/quote/list-index', ['as' => 'quote.list-index', 'uses' => 'Admin\\QuoteController#listIndex']);
Route::resource('/quote', 'Admin\\QuoteController');
Route::get('/playlist-song-category/list-index', ['as' => 'playlist-song-category.list-index', 'uses' => 'Admin\\PlaylistSongCategoryController#listIndex']);
Route::resource('/playlist-song-category', 'Admin\\PlaylistSongCategoryController');
Route::get('/playlist-song/list-index', ['as' => 'playlist-song.list-index', 'uses' => 'Admin\\PlaylistSongController#listIndex']);
Route::resource('/playlist-song', 'Admin\\PlaylistSongController');
Route::get('/album/list-index', ['as' => 'album.list-index', 'uses' => 'Admin\\AlbumController#listIndex']);
Route::resource('/album', 'Admin\\AlbumController');
Route::get('/artist/list-index', ['as' => 'artist.list-index', 'uses' => 'Admin\\ArtistController#listIndex']);
Route::resource('/artist', 'Admin\\ArtistController');
Route::get('/article/list-index', ['as' => 'article.list-index', 'uses' => 'Admin\\ArticleController#listIndex']);
Route::resource('/article', 'Admin\\ArticleController');
});
When you want to use route caching, you can't use closure to register routes in any file.
As you still have the default route from the fresh Laravel installation in your routes/web.php file, you get this error because when you do php artisan route:cache, Laravel under the hood selializes the routes files and combine them in a single one as he lookup would be quicker.
To solve the issue, you can simply remove the route if that is unneeded or move it to a controller like you did for all the others routes.
The error should be gone then.
Simply delete any route with a callback function like the default routes.
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
edit in
Route::middleware('auth:api')->get('/user', 'SomeController#someMethod');
Related
Trying to send data with axios got the error 404 not found laravel/vue
I am trying to save data from Vue.js to a Laravel controller with axios, but I am getting this 404 not fund error when I try with postman and on console. After I triend to change the axios path to: axios.post('storeInvoice' , {item: this.selectedItem , invoice_id:this.invoice_id}).then(response=>{ console.log(response); }) I got 405 Method not allowed web.php Route::post('storeInvoice', [InvoiceController::class , 'storeInvoice']); Items.vue axios.post('http://127.0.0.1:8000/storeInvoice' , {item: this.selectedItem , invoice_id:this.invoice_id}).then(response=>{ console.log(response); }) InvoiceController.php public function storeInvoice(Request $request){ dd('added'); //just for testing purpose } full web.php <?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\CategoryController; use App\Http\Controllers\CustomerController; use App\Http\Controllers\ItemController; use App\Http\Controllers\InvoiceController; use App\Http\Controllers\CompanyDetailsController; Route::get('/', function () { return view('welcome'); }); Auth::routes(); Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home'); Auth::routes(); Route::get('/home', 'App\Http\Controllers\HomeController#index')->name('home')->middleware('auth'); Route::group(['middleware' => 'auth'], function () { Route::resource('categories', CategoryController::class); Route::resource('customers', CustomerController::class); Route::resource('items', ItemController::class); Route::resource('invoices', InvoiceController::class); Route::get('invoice/export', [InvoiceController::class , 'export'])->name('exportInvoice'); Route::get('item/export', [ItemController::class ,'export'])->name('exportItem'); Route::get('customers/export', [CustomerController::class ,'export'])->name('exportCustomer'); Route::post('storeInvoice', [App\Http\Controllers\InvoiceController::class , 'storeInvoice']); }); Route::group(['middleware' => 'auth'], function () { Route::resource('user', 'App\Http\Controllers\UserController', ['except' => ['show']]); Route::get('profile', ['as' => 'profile.edit', 'uses' => 'App\Http\Controllers\ProfileController#edit']); Route::put('profile', ['as' => 'profile.update', 'uses' => 'App\Http\Controllers\ProfileController#update']); Route::put('profile/password', ['as' => 'profile.password', 'uses' => 'App\Http\Controllers\ProfileController#password']); });
Laravel Route [dashboard] is not defined
Trying to play with Laravel today for the first time. I am getting the following error when I attempt to visit: InvalidArgumentException Route [dashboard] not defined. routes/web.php Route::get('/', ['as' => '/', 'uses' => 'LoginController#getLogin']); Route::post('/login', ['as' => 'login', 'uses' => 'LoginController#postLogin']); Route::get('/logout', ['as' => 'logout', 'uses' => 'LoginController#getLogout']); Route::group(['middleware' => ['authenticate', 'roles']], function (){ Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'DashboardController#dashboard']); }); LoginController.php class LoginController extends Controller { use AuthenticatesUsers; protected $username = 'username'; protected $redirectTo = '/'; protected $guard = 'web'; public function getLogin() { if (Auth::guard('web')) { return redirect()->route('dashboard'); } return view('login'); } public function postLogin(Request $request) { $auth = Auth::guard('web')->attempt([ 'username' => $request->username, 'password' => $request->password, 'active' => 1]); if ($auth) { return redirect()->route('dashboard'); } return redirect()->route('/'); } public function getLogout() { Auth::guard('web')->logout(); return redirect()->route('/'); } }
as like name(). You should use one of the two : Route::group(['middleware' => ['authenticate', 'roles']], function (){ Route::get('/dashboard', 'DashboardController#dashboard')->name('dashboard'); }); Or Route::group(['middleware' => ['authenticate', 'roles']], function (){ Route::get('/dashboard', [ 'as' => 'dashboard', 'uses' => 'DashboardController#dashboard']); }); After, you clear route cache with php artisan route:clear Final, you can use php artisan route:list to lists all routes and the action bind
Try this: Route::get('/dashboard','DashboardController#dashboard')->name('dashboard');
When you use as it names the route , so you have add two name dashbbaorddashboard because you use as and name Route::group(['middleware' => ['authenticate', 'roles']], function (){ Route::get('/dashboard', ['uses' => 'DashboardController#dashboard'])->name('dashboard'); }); This will work
Laravel 5 route same urls login or not
I am updating a site from Laravel 4 to 5. In L4 I had this set up: if(Sentry::check()){ Route::get('/', array('as' => 'school.home.index', 'uses' => 'school\AuthSchoolController#index')); else{ Route::get('/', 'school\SchoolController#index'); } Note the same url but different controllers depending on login or not. With L5 I cannot use the middleware tried this: Route::get('/', 'SchoolController#index'); Route::group(['middleware' => 'auth'], function() { Route::get('/', array('as' => 'school.home.index', 'uses' => 'AuthSchoolController#index')); }); But this just passes over the first and goes to the group, where it gets redirected to the login page and to the admin if logged in. So I think I need an if/else equivalent in the route based on login but Auth::user() for doesn't seem to work: if(Auth::check()){ Route::get('/', array('as' => 'school.home.index', 'uses' => 'AuthSchoolController#index')); } else{ Route::get('/', 'SchoolController#index'); }
Route::get('/', function() { if( Auth::check() ) { return app()->make('App\Http\Controllers\SchoolController')->callAction('index', []); } else { return app()->make('App\Http\Controllers\AuthSchoolController')->callAction('index', []); } });
Try reordering the routes like so: Route::group(['middleware' => 'auth'], function() { Route::get('/', array('as' => 'school.home.index', 'uses' => 'AuthSchoolController#index')); }); Route::get('/', 'SchoolController#index');
Laravel Middleware except with Route::group
I'm trying to create a group Route for the admin section and apply the middleware to all paths except for login and logout. What I have so far is: Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'authAdmin'], function() { Route::resource('page', 'PageController'); Route::resource('article', 'ArticleController'); Route::resource('gallery', 'GalleryController'); Route::resource('user', 'UserController'); // ... }); How would I declare exceptions for the middleware with the above setup?
Simply nest groups and then you can exclude specific routes: Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() { Route::get('login', 'AuthController#login'); Route::get('logout', 'AuthController#logout'); Route::group(['middleware' => 'authAdmin'], function(){ Route::resource('page', 'PageController'); Route::resource('article', 'ArticleController'); Route::resource('gallery', 'GalleryController'); Route::resource('user', 'UserController'); // ... }); });
You can also use laravel's withoutMiddleware method as below; Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'authAdmin'], function() { Route::resource('page', 'PageController'); Route::resource('article', 'ArticleController'); Route::resource('gallery', 'GalleryController'); Route::resource('user', 'UserController'); Route::get('login', 'AuthController#login')->withoutMiddleware([AuthAdminMiddleware::class]); Route::get('logout', 'AuthController#logout')->withoutMiddleware([AuthAdminMiddleware::class]); });
How to use Redirect::intended in laravel?
I want the route to bypass authentication filter after successfully login. So I use Redirect::intended. But it makes problem. Here is my code In LoginController#doLogin if (Auth::attempt($credentials)) { return Redirect::intended("home.index"); } In Routes Route::group(array('before' => 'auth'), function() { Route::resource('home', 'HomeController'); Route::get('/', 'HomeController'); }); My routes If I put the the home resource route without the auth filter, then it will work. (Not redirect to login route.).That code is given below Route::resource('home', 'HomeController'); Route::group(array('before' => 'auth'), function() { Route::get('/', 'HomeController'); }); /* No Problem with this code */ But I want to work with auth filter.I'm using laravel 4. Please help me...
Currently I use this: in filter.php Route::filter('sentry', function() { if (!Sentry::check()) { return Redirect::route('authLogin'); } }); so in route.php Route::get('auth/login/', array('as' => 'authLogin', 'uses' => 'AuthController#login')); Route::post('auth/login/', array('as' => 'authLogin', 'uses' => 'AuthController#login')); Route::group(array('before' => 'sentry'), function() { Route::get('user/', array('as' => 'user', 'uses' => 'UserController#index')); Route::get('user/index', array('as' => 'userIndex', 'uses' => 'UserController#index')); }); I preffer named routes.