Tesing Laravel API Routes using POSTMAN - php

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.

Related

API routes are not working in Laravel VueJS app

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).*$');

VueJS SPA routing is not working with Laravel

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

Laravel API Routing Middleware

I am running Laravel 5.4 and have my API routes setup with an API middleware that verifies an authentication token sent in the headers.
However, I want to avoid, or prevent the api/Login (route that generates the auth token) from being subject to the middleware.
Currently, in my API middleware, before any logic happens I have:
if(strpos($request->getUri(), 'Login')):
return $next($request);
endif;
I would like to remove checking if the route is the Login route before proceeding with the middleware logic. Is there a native way in Laravel to accomplish the above?
Note: all API routes are protected via an API middleware group which I have created in the Http/Kernel, then added the in the RouteServiceProvider.
You could add an except property in your middleware
Route::group(['middleware' => ['api'], 'except' => 'Login'], function () {
// Your Routes
});

Laravel application change session path dynamically

The laravel application url will be something like app.laravel.com\{clientName}. All the routes will be following the client_name, for example app.laravel.com\{clientName}\home, app.laravel.com\{clientName}\profile. Will load/ render the application depends on the clientName.
routes/web.php
Route::group(['prefix' => '{clientName}', 'middleware' => 'appclient'], function () {
Route::get('/', 'ClientController#index');
Route::post('login', 'Auth\LoginController#login');
Route::post('logout', 'Auth\LoginController#logout');
Route::get('home', 'HomeController#index');
});
In the appclient middleware
public function handle($request, Closure $next) {
$clientName = explode('/', $request->path())[0];
$client = Client::where('clientName', $clientName)->first();
if(!isset($client->id)) {
abort(404);
}
Config::set('session.path', "/$clientName");
return $next($request);
}
What I'm trying to achieve is set the session based on the clientName directory. When I login I'm getting TokenMismatchException.
First question
Can I store the session based on url with directory like app.laravel.com\{clientName} ?
Second Question
I saw there is a setting session.path, what above I tried is to use that approach. If that is possible, how can I fixed this issue? Is it a good idea to updating the session path in the middleware?
Appreciate any feed back or other approaches
UPDATE
Using Redis as session driver
In my further investigation the request session token every time generates new one
What I did is updated the session.path & session.cookie dynamically.
Config::set('session.path', "$clientName");
Config::set('session.cookie', $clientName.'_laravel_session');
This is currently working for me.

Use built in Laravel 5.2 auth and load SPA then Dingo API for all other routes

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();
});
});

Categories