In my application i have a laravel backend with two spas consuming api from it. Now i want to manage my routes by using wildcard routes where i give both routes prefixes before the wildcard route takes effect. Here is an example
Route::prefix('creditors')->group(function () {
Route::any('/{all}', function () {
return view('creditor');
})->where(['all' => '.*']);
});
Now the issue us if i visit something like /creditors/login the spa returns a 404 not found. I want my spa to start handling routing after "creditors/". How do i go about this?
First remove this code :
->where(['all' => '.*']);
Second use these commands :
php artisan cache:clear
php artisan route:cache
And finally try again.
Related
I have had some weird issues in the past with Laravel routing, however they could usually be fixed with some form of logical solution, eg. clearing the route cache, or a genuine user error...
But this one is very strange. I have been developing this site for a few weeks and using the /admin routes for some time, but today anything related to /admin or logging in is resulting in a 404 | not found from Laravel. And now even adding any new routes is not working, including very basic ones.
eg. the home page is routed like this:
Route::get('/', [App\Http\Controllers\FrontendController::class, 'home'])->name('home');
But adding:
Route::get('/test', [App\Http\Controllers\FrontendController::class, 'home'])->name('test');
To my web.php routes is also bringing me to a 404.
Just to explain my development environment, I am on Windows and I have tried serving this with both "php artisan serve", and also with XAMPP, but both are giving me the same issue. I won't bother explaining the XAMPP set up, because this was literally working yesterday and nothing has changed, but with php artisan serve it takes me to "http://127.0.0.1:8000/".. this doesn't require any special hacking.. it usually just works.
What have I done differently today?
So all I have done today is:
Added a new command with "php artisan make:command SomeCommandIStartedMaking"
This surely hasn't screwed anything up? I've even deleted the command and reset to my last commit just in case and still the routes are screwed.
In my windows "hosts" file I added "127.0.0.1 somedomain.com"
So this sounds like the kind of thing that would have an effect on routing, and it's where I first noticed the issue.. but since then I have "ipconfig /flushdns", I have removed the entries from my host file, and I have cleared my laravel routes and caches, but still the issue remains.
I am really stuck on this one. Also finally! I have done a route:list and I can clearly see the routes showing up like expected, but I can assure you that nothing has changed since yesterday so this seems like another issue.
Here is a dump of web.php, because I know this will be asked for:
<?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!
|
*/
// Frontend
Route::get('/', [App\Http\Controllers\FrontendController::class, 'home'])->name('home');
Route::get('/instagram-gallery', [App\Http\Controllers\FrontendController::class, 'instagramGallery'])->name('instagram-gallery');
Route::get('/{pageAlias}', [App\Http\Controllers\PageController::class, 'index'])->name('page');
Route::post('/contact-send', [App\Http\Controllers\PageController::class, 'contactSend'])->name('contact-send');
Route::get('/test', [App\Http\Controllers\FrontendController::class, 'home'])->name('test');
// Admin
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'is_admin']], function(){
Route::get('/', [App\Http\Controllers\AdminController::class, 'index'])->name('admin');
Route::get('/search/{table}', [App\Http\Controllers\AdminController::class, 'search'])->name("admin-search");
Route::get('/browse/{table}', [App\Http\Controllers\AdminController::class, 'browse'])->name("admin-browse");
Route::get('/create/{table}', [App\Http\Controllers\AdminController::class, 'create'])->name("admin-create");
Route::get('/edit/{table}/{id}', [App\Http\Controllers\AdminController::class, 'edit'])->name("admin-edit");
Route::get('/manage-account', [App\Http\Controllers\AdminController::class, 'manageAccount'])->name("admin-manage-account");
Route::get('/documentation/{subject?}', [App\Http\Controllers\AdminController::class, 'documentation'])->name("admin-documentation");
Route::post('/create/{table}/action', [App\Http\Controllers\AdminController::class, 'createAction'])->name("admin-create-action");
Route::post('/edit/{table}/{id}/action',[App\Http\Controllers\AdminController::class, 'editAction'])->name("admin-edit-action");
Route::post('/delete', [App\Http\Controllers\AdminController::class, 'deleteAction'])->name("admin-delete-action");
Route::post('/wysiwyg-upload', [App\Http\Controllers\AdminController::class, 'wysiwygUpload'])->name("wysiwyg-upload");
Route::post('/manage-account-action', [App\Http\Controllers\AdminController::class, 'manageAccountAction'])->name("admin-manage-account-action");
});
require __DIR__.'/auth.php';
One thing to add, is that "/contact" resolves just fine which is using the '/{pageAlias}' route as shown above, so some are working and some aren't. How should I trouble shoot this I don't even know where to start :(
Any help would be hot! This is so frustrating haha! Thanks in advance..
This might due to cache so run following commands
php aritsan config:clear
php artisan route:clear
To use single command:
PHP artisan optimize
The problem is the order of your routes - the code will run line by line trying to find a matching route.
Once it hits
Route::get('/{pageAlias}', [App\Http\Controllers\PageController::class, 'index'])->name('page');
It will match /test and naturally stop - never reaching your next route.
To solve this, your specific route /test needs to be defined before your general "catch-all" route /{pageAlias}
Sorry - was difficult to give this one a clear title! But I have an issue and a difference between how my local laravel install is dealing with some routes compared to my live server.
Locally, I have this working:
Route::get('/blog', 'BlogController#home');
Route::get('/blog/{post_slug}', 'BlogController#viewPost');
As you can probably guess, I want to serve up a list of posts via the home() function if /blog is hit. Then all other traffic with a "slug" after /blog/, I want to load the blog post.
This all works locally.
However on live,
/blog/my-blog-post
Is serving up the home() function every time.
Where would I start with debugging this. Laravel versions? Server caching?
Maybe you can do this in laravel 5.7+
Route::prefix('blog')->group(function () {
Route::get('/', 'BlogController#home');
Route::get('/{post_slug}', 'BlogController#viewPost');
});
before just use: php artisan optimize, to clear all cache route and config.
for more info see the docs
LogicException : Unable to prepare route [api/user] for serialization. Uses Closure.
at /var/www/html/dev_laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php:917
913| */
914| public function prepareForSerialization()
915| {
916| if ($this->action['uses'] instanceof Closure) {
> 917| throw new LogicException("Unable to prepare route [{$this->uri}] for serialization. Uses Closure.");
918| }
919|
920| $this->compileRoute();
921|
Exception trace:
1 Illuminate\Routing\Route::prepareForSerialization()
/var/www/html/dev_laravel/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteCacheCommand.php:62
2 Illuminate\Foundation\Console\RouteCacheCommand::handle()
/var/www/html/dev_laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32
When i trying to run the laravel command
php artisan route:cache
I try to find out the solution but not get the correct solution.
https://github.com/laravel/framework/issues/22034
Is this laravel bug still or fixed the bug?
I have the code on web.php file
Route::get('/', function () {
return view('welcome');
});
Route::resource('photos', 'PhotoController#index');
I'm Using Laravel 5.8. Just installed and migrated database. I'm beginner for laravel.
Can anyone let me know the correct solution?
Thanks in advance
It is not a bug. You can not use Closure based routes like that if you want to cache the routes. Direct any Closure based route to a Controller method instead and you will be fine. [You have one in web.php and the error is pointing out one in api.php]
The Closure based route you have in web.php could be replaced with:
Route::view('/', 'welcome');
This would direct it to a Controller that just handles returning view files.
The Closure based route in api.php should point to a Controller:
Route::middleware('auth:api')->get('user', 'SomeController#user');
Consider any routes that come with the laravel/laravel project as being functional but are there for demonstration purposes.
"Closure based routes cannot be cached. To use route caching, you must convert any Closure routes to controller classes."
Laravel 5.8 - Docs - Controllers - Route Caching
EDIT:
AS OF LARAVEL 8.X YOU CAN ALSO CACHE CLOSURE BASED ROUTES
When you run the command php artisan route:cache
Laravel will cache all your routes and store it in specified Cache Driver
Now Comming to your Error Message:
As the error Message Clearly Says that
Closure Routes can't be Cached
And even the Laravel Docs says that Route Caching
By Default Laravel Comes with Four routes files
And this will have 2 Closure based routes
Solution:
You can remove them if you no longer using that routes
Make a Controller and Move that to Controller
LogicException : Unable to prepare route [api/user]
Which means that
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
This Code in Your routes/api.php is causing the issue So
Route::middleware('auth:api')
->get('/user', 'SomeController#method');
Or you can remove that if not using that
You could not use specific method when you use resource for the routing.
You can use
Route::resource('photos', 'PhotoController');
Or
Route::get('photos', 'PhotoController#index');
I'm building and application in Laravel and Vuejs where I'm having Laravel routes as below:
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->where('view', '!=', 'admin')->name('home');
I'm using Vue-router so I'm having routing in vuejs, and I'm using history mode. The problem is when I try to call /admin it generally calls HomeController#home method. even if I go deeper like /admin/dashboard it is calling the same home method. I want if admin prefix is being called then it should call HomeController#admin method.
its all okay for me please check this
Route::get('/admin/{view?}', function (){
dd('okay');
})->where('view', '(.*)')->name('admin');
Route::get('/{view?}', function(){
dd('okay1');
})->where('view', '(.*)')->name('home');
So try this
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->name('home');
I'm all set up with Passport in 5.5 and have the auto generated Auth\ForgotPasswordController and Auth\ResetPasswordController controllers.
However whereas /oauth/token was provided magically for me, there don't appear to be such routes for password reset when using the API.
What should my API routes look like?
Currently I've experimented with
Route::group(['prefix' => 'password'], function () {
Route::post('/email', 'Auth\ForgotPasswordController#sendResetLinkEmail');
Route::post('/reset', 'Auth\ResetPasswordController#reset');
});
but I found these in the vendor files when looking at the traits and aren't sure if this is the correct way.
The /password/email route also fails with "message": "Route [password.reset] not defined."
since you don't see any route other then 2 custom, therefore i am assumin you havn't run artisan auth command. First run that. it will add lot of routes in ur project.
Then set api driver to passport.