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
Related
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?
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
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
I have the following route declaration:
Route::name('test.')
->prefix('test')
->group(function() {
Route::get('/', function() {
dd('test');
})->name('index');
});
Trying to access /test will result in NotFoundException, although when calling route('test.index') it will resolve to /test.
Same exception when trying to access /test/ and when trying to change the line:
Route::get('/', function()...
to
Route::get('', function()...
As soon as I modify it to
Route::get('/test', function()...
it works when trying to access ´/test/test´ and it also resolved the uri correctly when calling route('test.index').
What am I missing to to get the route working using '/' ?
Default route is running
for example:
Route::get('/', function () {
return view('welcome');
});
When i run this http://localhost/laracast/public
Shows outputs as LARAVEL 5
But when I add new route to
Route::get('/hello', function() {
return 'Welcome to Laracast';
});
It shows output as => url not found
I am using
1.Windows7
2.Wamp Server
3.Composer and
4.GIT BASH
what the mistake i done it
If you are changing the default route to /hello, you need to make sure you visit http://localhost/laracast/public/hello in the browser.
If you are just trying to display your own message, change the default route to:
Route::get('/', function() {
return 'Welcome to Laracast';
});
i.e. remove hello from the route and it will correctly display your welcome message when visiting http://localhost/laracast/public