How to log all routes acess? - laravel

I build api service using laravel.
I want to log all acess to the api routes
I though somewhere in the routes.php put some code that get the requested route? any help? thanks
laravel 4

You can define a route filter first
Route::filter('log', function($route, $request, $response)
{
// log work
});
then apply the filter to your route
Route::get('api', array('before' => 'log', function()
{
return 'logged!';
}));
I think you can also get the log from the access log of your web server.

Related

How can I use Inertia to make a fetch request to a route behind `auth:api` middleware?

I have a Laravel app with a number of routes behind auth:api. I'd like those routes to be available to users in my Dashboard.
I've tried the following on a route that I have named api.me that is behind auth:api, but Inertia does not provide the Authorization header.
import { useForm } from "#inertiajs/inertia-react";
...
const { data, setData, errors, get } = useForm({});
...
get(route('api.me'));
I assume the route must be behind the auth middleware instead. If so, is it possible to have it use both the auth and auth:api middlewares?
It looks like the best way to handle this is to provide the data via the controller or route.
The route should return:
Route::get('/subscription', function() {
return Inertia::render('some/route', [
'me_info' => Auth::user(),
'other_data' > ['some' => 'other data']
]);
});
If you are using React, the me_info and other_data will be made available to the component props.

Laravel router problem when I use Route::any

I would appreciate any help you could give me with the following problem I have.
I am trying to implement a Single Page Application (SPA) with Laravel and Vue, at the moment I am defining the routes, I have something like the following:
routes/app.php
Route::group(['middleware' => 'web'], function () {
Route::any('{path}', function () {
return view('index');
});
});
What I'm trying to do here is the following:
For the middleware web you will only have one view available, in this case in the file index in (resources/views/index.php)
I want that regardless of the method used, Laravel returns the view I want. But I have not succeeded.
Output of php artisan route:list
And when I try to verify with Postman I receive a 404 in response regardless of the method or the URL that I requested .. what could I be doing wrong? Thank you very much in advance.
EDIT 1
I have modified my router and added a conditional as follows:
Route::group(['middleware' => 'web'], function () {
Route::any('{path}', function () {
return view('index');
})->where('path', '.*');
});
Now the GET and HEAD method work properly, however ... when I try the POST method, I get the following error ..
EDIT 2
As mentioned #lagbox in the comments below, error 419 refers to missing CSRF token. I could disable this check in the file Middleware/VerifyCsrfToken.php.
However I wish I could just return the desired view .. without first checking the CSRF token. Another detail that I find is the following:
When in Postman I make a request by a method different from the typical methods ... let's say UNLINK method, I get the following result:
Which leaves me a bit confused, when I define in my routes file web.php Route::any, does it mean any route or not?

How to integrate role based permission in Laravel with Dingo API?

I'm currently studying Laravel framework and dingo api. Is there any way to integrate the role based permission using entrust to dingo api?
So for example, I have a route to get all the list of users, but only admin can access this.
So if the user is authenticated, but he's not an admin, he can't access this route.
I tried adding the middleware of entrust to the routes.php but when I tried it on postman, I get a syntax error.
here's my routes.php file:
$api->version('v1', ['middleware' => ['jwt.auth', 'role:admin']], function ($api) {
$api->get('users', 'App\Http\Controllers\Auth\AuthController#index');
$api->get('user', 'App\Http\Controllers\Auth\AuthController#show');
});
You can group this into different parts as this:
$api->version('v1', ['middleware' => 'jwt.auth'], function ($api) {
//general routes route goes here
//....
$api->group(['middleware' => 'role:admin'], function($api) {
//admin routes goes here
$api->get('users', 'App\Http\Controllers\Auth\AuthController#index');
$api->get('user', 'App\Http\Controllers\Auth\AuthController#show');
});
});
This means even though the user is authenticated, the two routes in the new group can only be accessed by the admins.
I hope this is helpful.

Laravel route group page redirects to home

I have installed Laravel and set up authentication and I have also created a route group like this:
// users that want to access test route should be logged in.
Route::group(['middleware' => ['web', 'auth']], function () {
Route::get('first', function () {
return 'first';
});
});
The problem is when I access the route like this:
http://localhost/first
I can see my "first" message, but when I refresh the same page laravel redirects me to:
http://localhost/home
I could not solve this and I have moved my first route out of the route group now everything is working well. If I keep it in the route group with auth & web middlewares it is not working.
Try to remove web middleware if you're using 5.2.27 and higher.

Handle every request in laravel 5

I have laravel project and I want to create access log table. in route file is it possible handle every request and its parameters to store in database.
You can create middleware and handle all request with it. Then put all your routes in a group to apply your middleware.
Route::group(['middleware' => 'yourMiddleware'], function () {
// All your routes
});
Yes, it is possible. Create your own service provider and register it, then in boot method create script that logs requests to database.
Example:
public function boot()
{
if (! app()->runningInConsole()) {
App\Request::create(['payload'=>serialize(app('request')->all())]);
}
}

Resources