I'm building a SPA with Laravel and Vue. However, I need to make an API call with axios but it's not working.
Here's my web.php:
Route::get('/{any}', function () {
return view('index');
})->where('any', '.*');
Here's my api.php route:
Route::get('/notification', 'NotificationController#index');
Whenever I make an API call, it shows a blank or broken page.
Could you please give me any suggestion about this issue and solve?
You should prevent api routes from being captured by web.php route.
First, prefix api routes:
api.php
Route::get('/api/notification', 'NotificationController#index');
Then, exclude prefixed routes from web routes:
web.php
Route::get('/{any}', function () {
return view('index');
})->where('any', '^(?!api).*$');
Related
i have a project that uses API and Web routes
web.php:
Route::middleware(['auth:sanctum',config('jetstream.auth_session'),'verified'])->group(function () {
Route::get('/admindashboard', function () { return view('dashboard');})->name('dashboard');
});
the above is working fine and i can call auth()->user() inside the blade
but here in
api.php:
Route::prefix('orders')->as('orders.')->controller(OrderController::class)->group(function(){
Route::get('index', 'index')->name('index');
});
there is no middleware and i can't use auth()->user() inside its blade
how can i wrap the above routes (in api.php) with middleware so i can use auth in balde?
Actually, it's the same middleware that you can use in apis as well.
Route::middleware('auth:sanctum')->group(function () {
// Authenticated Routes in api.php
Route::get('/user', function ()
{
return auth()->user();
});
});
I am trying to setup a standard homepage that every user has access to (and lands on), before they are routed to other pages after logging in.
Currently the web.php setup is
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'home']->withoutMiddleware(['auth']));
The withoutMiddleware is to try and bypass the requirement of auth when trying to access the home.blade.php page, however I get the following error:
Very new to Laravel, so any help would be greatly appreciated. Cheers!
Routes don't require authentication unless it's explicitly specified in the routes web.php file or the controller.
So it can be as following:
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'home'];
Route::get('/', function () {
if(Auth::guest()){
return view('/home');
}else{
return redirect('/login');
}
});
Im trying to build SPA and I need to make axios call to endpoint in my routes. How can make this api call work?
Here are my routes
Route::get('/{any}', function () {
return view('default');
})->where('any', '.*');
Route::get('events', 'EventsController#index')->prefix('api');
Any suggestion?
You must go to the folder routes->api and create your api routes, there is automatically the api prefix.
in your .vue
axios.get('/api/getUser')
.then(data => {
console.log(data)
});
in your folder routes->api
Route::get('/getUser', 'Api\UsersController#getUser');
To not put "/api" in front of all your axios calls in your '.vue' you can put
axios.defaults.baseURL = '/api';
in your app.js
We define a catch-all route to the SpaController which means that any web route will map to our SPA. If we didn’t do this, and the user made a request to /hello, Laravel would respond with a 404.
Route::get('/{any}', 'SpaController#index')->where('any', '.*');
Remember that this route will always be putted on the last part of your web routes after declaring your other routes
use this
Route::any('{all}', function () {
return view('default');
})
->where('all', '^(?!api).*$')
->where('all', '^(?!storage).*$');
Route::get('events', 'EventsController#index')->prefix('api');
as you will need api and storage route handel by laravel
I am very sorry if my question sounds foolish. I am new to Laravel. I develop some API in Laravel to be used in Angular, but a bit confused about what to type in POSTMAN in order to test the APIs. The project name is 'ntiapi'
I have written the API in the Controller. Also, I have done the api.php in routes
api.php
Route::post('/register', 'AuthController#register');
Route::post('/login', 'AuthController#login');
Route::post('/logout', 'AuthController#logout');
Route::get('/tasks', 'TaskController#index')->name('tasks.index');
Route::post('/tasks', 'TaskController#store')->name('tasks.store');
Route::get('/tasks/{task}', 'TaskController#show')->name('tasks.show');
Route::put('/tasks/{task}', 'TaskController#update')->name('tasks.update');
Route::delete('/tasks/{task}', 'TaskController#destroy')->name('tasks.destroy');
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
I have this http://localhost:ntiapi/api/
How do I add each of the routes to http://localhost:ntiapi/api/
in order to test the api on the POSTMAN. I mean the complete url. For example http://localhost:ntiapi/api/login.
If there is no route prefix or group prefix it should be
http://localhost/ntiapi/public/api/login
and if you are running artisan server (php artisan serve)
it should be http://127.0.0.1:8000/api/login or http://localhost:8000/api/login whatever you prefer.
I'm trying to work out if it's possible to use the regular Laravel Authentication routes with blade/views for basic Auth then load the SPA (Vue.js with it's own router) and make calls to the API via Dingo?
At the moment I have this at this top of my routes.php, which works:
// All routes through web middleware
Route::group(['middleware' => 'web'], function () {
// Authentication
Route::auth();
// Authenticated routes
Route::group(['middleware' => 'auth'], function () {
// Load main SPA
Route::get('/', 'AppController#spa');
});
});
app.domian.com/ is protected with Auth and thats the route which the SPA uses. I use the standard, built in, Laravel Auth pages (non SPA) so when the user is logged in or registered it allows access to the home route and loads the SPA.
What I would like to do is use Dingo from this point onwards. So calls to app.domian.com/api/* are all handled by Dingo.
I've added this to the same routes file:
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function($api) {
$api->group(['middleware' => 'auth'], function ($api) {
// API prefix: api
$api->get('user', function($api) {
return Auth::user();
});
});
});
This doesn't seem to work.
It is even possible to use Dingo in this way or do I have to forfeit the built in Auth for something like JWT. I'm hoping to do that in the future, but for now I just need to get this working.
you need to replace auth with api.auth in middleware.
$api->group(['middleware' => 'api.auth'], function ($api) {
// API prefix: api
$api->get('user', function($api) {
return Auth::user();
});
});