How to remove Parameter From All Request Objects at Controller Level?

0 votes

I have URLs that look like:

http://example.com/api/user?id=45&name=mike&api_token=2348283
http://example.com/api/project?id=5&description=first&api_token=2348283
etc...

In my controllers, I have functions that look like:

public function user_get_endpoint(Request $request){

    $request = $request->toArray();
    return UserModel::where($request)->get()->toArray();

}

The above will currently break since the $request object contains a property called api_token which does not exist in the user table. I am using the api_token in a middleware to check for authentication.

I can manually unset the api_token property in each of my API functions by using unset($request['api_token'], but I'd like to avoid that if possible.

Is there anyway to do this application wide or at a class or controller level?

Oct 20 in Laravel by kartik
• 35,300 points
70 views

1 answer to this question.

0 votes

Hello @kartik,

First arrange for the middleware to run on all routes:

// routes.php
$app->middleware([
    App\Http\Middleware\Apitoken::class
]);

Then define what the middleware should do:

// src/App/Http/Middleware/Apitoken.php
<?php
namespace App\Http\Middleware;

use Closure;

class Apitoken
{
    public function handle($request, Closure $next)
    {
        unset($request['api_token']);

        return $next($request);
    }
}

Hope it helps!!

answered Oct 20 by Niroj
• 76,540 points

Related Questions In Laravel

0 votes
1 answer

How to remove composer sdk from laravel packages

Use  two blade page,one for create and other ...READ MORE

answered Oct 15 in Laravel by Niroj
• 76,540 points
48 views
0 votes
1 answer

How do I write to the console from a Laravel Controller?

Hello @kartik, This can be done with the ...READ MORE

answered Oct 29 in Laravel by Niroj
• 76,540 points
29 views
0 votes
1 answer

How to pass data through URL and access through controller in Laravel?

Hello, You can  first refer how to  Create controller through ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,540 points
3,015 views
0 votes
1 answer

How to identify wheather the request is HTTP GET or HTTP POST in Laravel?

Hey, In order to identify the type of ...READ MORE

answered Mar 19 in Laravel by Niroj
• 76,540 points
131 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,540 points
3,441 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,540 points
411 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,540 points
281 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,540 points
4,142 views
0 votes
1 answer

How to change value of a request parameter in laravel?

Hello @kartik, Use merge(): $request->merge([ 'user_id' => ...READ MORE

answered Aug 10 in Laravel by Niroj
• 76,540 points
274 views
0 votes
1 answer

How do I write to the console from a Laravel Controller?

Hello @kartik, This can be done with the ...READ MORE

answered Aug 11 in Laravel by Niroj
• 76,540 points
93 views