laravel 7 php artisan route:cache is not working - laravel

Look at my code, Please.
web.php
Auth::routes();
//
Route::get('/', 'HomeController#index')->name('home');
Route::get('/findIDProvince', 'HomeController#findIDProvince')->name('findIDProvince');
Route::get('/markets', 'MarketController#index')->name('market');
Route::get('/market/{marketSlug}', 'MarketController#single');
Route::get('/category/{categorySlug}', 'CategoryController#single');
Route::match(['get', 'post'],'/cart/{market}',"MarketController#AddCard")->name('cart');
Route::get('/shopping-cart', 'MarketController#showCart')->name('cart');
Route::post('/comments', 'MarketController#comments')->name('comments');
Route::get('/{pageSlug}', 'PageController#contact')->name('contact');
Route::middleware('auth:web')->group(function () {
Route::post('/payment', 'PaymentController#payment')->name('payment');
Route::get('/payment/callback', 'PaymentController#callback')->name('payment.callback');
});
Route::prefix('ajax')->group(function() {
Route::post('/add-to-cart', 'AjaxController#add_to_cart');
Route::post('/remove-from-cart', 'AjaxController#remove_from_cart');
Route::post('/get-cart', 'AjaxController#get_cart');
Route::post('/increment-cart-item', 'AjaxController#increment_cart_item');
Route::post('/decrease-cart-item', 'AjaxController#decrease_cart_item');
Route::delete('/delete/{id}', 'AjaxController#delete');
});
Route::namespace('Admin')->middleware(['auth:web', 'checkAdmin'])->prefix('admin')->group(function (){
Route::get('dashboard', 'DashboardController#index')->name('dashboard');
Route::resource('slideShows', 'SlideShowController');
Route::resource('categories', 'CategoryController');
Route::resource('users', 'UserController');
Route::resource('markets', 'MarketController');
Route::resource('orders', 'OrderController');
Route::resource('pages', 'PageController');
Route::get('footers', 'FooterController#index')->name('footers.index');
Route::get('links', 'LinkController#index')->name('links.index');
Route::post('links/store', 'LinkController#store')->name('links.store');
Route::resource('address', 'AddressController');
Route::get('socials', 'SocialController#index')->name('socials.index');
Route::post('socials/store', 'SocialController#store')->name('socials.store');
Route::get('approved', 'CommentController#approved')->name('approved');
Route::get('unapproved', 'CommentController#unapproved')->name('unapproved');
Route::put('comment/update/{comment}', 'CommentController#update')->name('comment.update');
Route::delete('comment/destroy/{comment}', 'CommentController#destroy')->name('comment.destroy');
});
I have installed laravel 7 on my local server. When I run php artisan route:cache command then laravel returns the error:
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.

Laravel can't cache routes, which use closures - https://github.com/laravel/framework/issues/22034
There is example user route in routes/api.php Just remove it and try again

Related

Laravel - lumen : Unable to resolve route handler localhost - MAC os

I was following a tutorial to getting started with lumen project for APIs,
Routes/web.php
// Working
Route::get('/', function () use ($router) {
return $router->app->version();
});
// Error - Unable to resolve route handler
Route::group(['prefix' => 'api'], function () {
Route::get('template', [TemplateController::class, 'index']);
});
Out put of http://localhost:8000/api/template
Versions - PHP 8.0, Lumen 9.0, XAMPP 8.0.25,
OS - MAC
Run the project php -S localhost:8000 -t public/ command
Please help me to solve this problem.
Thank you

Laravel 9 - why adding ['middleware' => 'auth'] to route::group causes "require(auth): Failed to open stream: No such file or directory" error

I'm new to php and Laravel. I'm using Laravel 9 with Fortify and some custom login ui. They are working correctly. The home page nav bar is showing the Login and Register links if user is not authenticated. The Login and Register links are working fine as well. What I'm trying to do now is to auto route user to the Login page if the user is not authenticated. So below is what I did.
In my web.php, I've this line to route all admin path to routes\admin\adminRoutes.php.
Route::prefix('/admin')->group(__DIR__ . '\admin\adminRoutes.php');
In the routes\admin\adminRoutes.php, I have this:
Route::name('admin.')->group(['middleware' => 'auth'], function () {
Route::get('/', [HomeController::class, 'index'])->name('home');
Route::get('/home', function () {
return redirect()->route('home');
});
});
Basically, I added the ['middleware' => 'auth'] to the group function. But for some reason, this cause the site to failed with this error that I don't understand:
require(auth): Failed to open stream: No such file or directory
What is that means and what did I do wrong?

API routes return 404 in laravel

I have https://tenancyforlaravel.com/ installed in laravel to make multi-tenant and it works fine for the web routes.
My problem is that when I access my APIs then I get a 404 error in tenant domains.
tenancyforlaravel documentation: https://tenancyforlaravel.com/docs/v3/routes
It says that I must put all my APIs inside api.php file and wrap them in a Route group with this middleware so I put all my APIs inside api.php file and all my APIs as below:
Route::middleware('tenancy')->group(function () {
Route::name('api.')->namespace('Api')->group(function () {
Route::post('/login', 'AuthController#login')->name('login');
...
});
and when I access it using sub.local.test/api/login then I get 404 error.
Tested for tenancyforlaravel.com V3 and it works OK.
Route::middleware([
InitializeTenancyByDomain::class,
PreventAccessFromCentralDomains::class
])->prefix('api')->group(function () {
//
Route::name('api.')->namespace('App\Http\Controllers\Api')->group(function () {
Route::post('/login', 'AuthController#login')->name('login');
...
});
Put all your API routes inside api.php as below
use App\Http\Controllers\AuthController;
Route::group(['prefix' => '/{tenant}',
'middleware' => [InitializeTenancyByPath::class],],
function () {
Route::post('/login', [AuthController::class, 'login'])->name('login');
...
});
As you haven't mentioned your tenant identifier, I am using path as identifier, so using InitializeTenancyByPath middleware. Use whatever identifier middleware you want in place of that.
Access your API routes normally as you used to do, with your identifier. As this example uses path as identifier, the endpoint will look like:
sub.local.test/api/{tenant}/login

How to use Laravel authentication and vue-router together

I'm new to Laravel and PHP in general, but familiar with Vue and SPA's. I know how to create authentication with Bcrypt, for example. However, I used Laravel to run php artisan make:auth and to create several different endpoints for the backend.
I was trying to convert my app into a SPA using Vue, however using routing with Vue causes issues with the routes defined in the web.php. For example, I have my web.php like this, with several different routes.
<?php
Route::get('/{vue_capture?}', function () {
return view('app');
})->where('vue_capture', '^(?!storage).*$');
Route::get('/', function () {
return view('app');
});
Route::resource('Videos', 'VideoController')->middleware('auth','isAdmin');
Route::resource('Categories', 'CategoriesController')->middleware('auth');
Route::get('/search', 'VideoController#search')->middleware('auth');
Auth::routes();
Route::get('/settings/account', 'AccountsController#edit')->middleware('auth');
Route::get('/auth', 'AccountsController#get');
Route::put('/settings/account', 'AccountsController#update')->middleware('auth');
However, I also have a routes.js to have vue-router handle the routing with the following :
import Home from './components/Home.vue'
import Videos from './components/Videos.vue'
import Categories from './components/Categories.vue'
export default{
mode: 'history',
routes: [
{
path:'/',
component:Home
},
{
path:'/Videos',
component:Videos
},
{
path:'/Categories',
component:Categories
},
{
path:'/login',
component:login
},
{
path:'/register',
component:register
},
{
path:'/logout',
component:logout
}
]
}
I understand that Vue is supposed to take over the routing if you use a SPA, but is there any way to use both? I can't find anything that addresses this issue but I can't believe that Laravel would make you choose to either use their built-in commands like php artisan make:auth or have to scrap all of that and do it all manually if you want a SPA with routing.
I've tried going through different CRUD turorials on using Laravel and or Vue.
I've tried directing all of the routes to Vue to have vue-router handle the routing.
I've tried not giving all routing to vue-router and retaining the back-end routes that I had in web.php
Other semi-related problem is my routes aren't even appearning as links using router-link . It just appears as normal text. But for now my priority is the routing issue.
You can combine both vue route and Laravel route. But for the best result, I advise you use vue router since you are building an spa.
remove
Route::get('/', function () {
return view('app');
});
put all the backend route before the route that point to your vue.
Route::resource('Videos', 'VideoController')->middleware('auth','isAdmin');
Route::resource('Categories', 'CategoriesController')->middleware('auth');
Route::get('/search', 'VideoController#search')->middleware('auth');
Auth::routes();
Route::get('/settings/account', 'AccountsController#edit')->middleware('auth');
Route::get('/auth', 'AccountsController#get');
Route::put('/settings/account', 'AccountsController#update')->middleware('auth');
Route::get('/{vue_capture?}', function () {
return view('app');
})->where('vue_capture', '^(?!storage).*$');
Also, verify that you don't have conflicting routes on your backend (run php artisan route:list to see your laravel route list) and vue routes. I hope this helps.
The accepted answer is perfect (I upvoted but I have no rep yet for it to count) and worked well for me but only under the condition if I also created 'dummy' vue components and imported them to work with the routes. Hope this helps!
import Login from './auth/Login'
import Register from './auth/Register'
import Logout from './auth/Logout'
{ path:'/login', component:Login },
{ path:'/register', component:Register },
{ path:'/logout', component:Logout },
You can just use this instead
Route::get('/{vue_capture?}', function () {
return view('app');
})->where('vue_capture', '^(?!storage).*$')->middleware('auth');

Why don't catch the requested file URI my defined Route in Laravel 5.7?

I have this codes in my routes/api.php file:
Route::group(['middleware' => 'auth:api'], function () {
Route::prefix('photoalbum')->group(function() {
Route::prefix('image')->group(function() {
Route::post('download/{albumId}/{size}/{filename}',
'PhotoalbumImageController#download');
// ...
});
});
});
Route::fallback('HomeContorller#index');
Now I try to open this URL:
http://myproject.test/api/photoalbum/image/download/1/xs/dog.jpg
...and I get the result from the HomeController#index function. The other routes working fine.
UPDATE
The php artisan route:list get the correct list of routes, contain this:
| | POST | api/photoalbum/image/download/{albumId}/{size}/{filename} | | App\Http\Controllers\PhotoalbumImageController#download | api,auth:api,auth |
Additionally: the requested file isn't exists. The controller should be process and serve it.
Why don't catch the request my defined Route and send it to the PhotoalbumImageController#download function and how can I fix it?
Your defined route type is POST and you are trying to access that via GET.
changing your route to Route::get solves your problem.
Please try this, and use name for routes it is useful, and remember if the call is GET, POST, PUT, etc.
Route::group(['middleware' => 'auth:api','prefix'=>'photoalbun/image'], function () {
Route::match(['post','get'],'/download/{albumId}/{size}/{filename}','PhotoalbumImageController#download')->name('api.photoalbun.image.download');
});
To see all route you can use
php artisan route:list

Resources