index "/" route not working when using with a prefix in Laravel - laravel

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 '/' ?

Related

How to send to laravel request with api prefix?

location #laravelapi {
rewrite /api/(.*)?$ /api/index.php?$is_args$args last;
}
When we try access to api/routeexample, laravel gets only routeexample part, and throws 404 error. How we can send full url including 'api' part?
I m adding an example of how we define route for API request:
In routes/api.php
Route::group(['prefix' => 'v1'], function () {
Route::post('api_request', [App\Http\Controllers\YourController::class, 'targeted_function_name'])->name('api_request');
});
So I'm just added
include "api.php";
into my routes/web.php as hotfix, but i will search for better solution

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

Axios Get request return html not data SPA

need some help. I'm trying to fetch data from my db using axios. My backend is Laravel. I have a 200 status http request but it returns the whole html not the data I'm expecting.
Here is my code for route
Route::get('/home', 'PostController#ajaxCall');
Route::post('/home', 'PostController#store');
Route::get('/{any?}', function () {
return view('welcome');
});
Here is my code for Home.vue for Axios request
export default {
components: {addForm},
data () {
return{
posts:[]
}
},
created() {
axios.get('/home').then(response => this.posts = response.data);
}
}
For my controller
public function ajaxCall(){
return response(Post::all());
}
It looks like you get to the ajaxCall() method by using the route '/home', but with axios, you are hitting "/" which returns a view called Welcome. Maybe you need to change the path you use in axios to '/home'?
It might be late but maybe someone else is looking for the solution
I was also facing this in SPA using laravel and VUE.
Route::get('/{any}', 'SinglePageController#index')->where('any', '.*');
SPA has this route, so whenever you write any other get route your app will redirect you to the home page, to avoid this either move this route at the end of your web.php
or write your other routes in api.php file.
In my case i solved it by changing the GET method to POST method.
Try that. It might help

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

Laravel 5.2: url not found but router is available in windows 7 wamp server

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

Resources