Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rniederer's avatar

Problem with route, params and middleware

Hello

I developed my first Laravel-project - an api.

Local works all fine. But on production server (hosting) I have a problem with my middleware, route and their params.

From my api.php

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});
Route::put('wishlist/{id}', 'WishlistController@update')->middleware(['auth:api']);

My problem is, that i can locally read my param with:

public function update(int $id, Request $request)

I can use "int $id" to get route param. But on production environment it doesn't work.

There i have to use this: public function update(Request $request)

But how can i get the route param now?

0 likes
8 replies
MichalOravec's avatar

Change it to this

public function update(Request $request, $id)

So just change order of your params.

Lathams's avatar

Localized web applications often set the locale (and therefore the language) based on a routing parameter, the session, or a specialized sub-domain. In this recipe we will concentrate on introspecting the URI path via middleware, which allows you to have a global mechanism for detecting the locale without requiring any changes to existing routes.

https://www.paygonline.vip/

rniederer's avatar

All APIs with route params doesn't work.

So I can insert a new entry, but I can't update an existing:

Route::middleware('auth:api')->put('wishlist/{id}', 'WishlistController@update');
Route::middleware('auth:api')->post('wishlist', 'WishlistController@insert');
rniederer's avatar

my middleware (app/Http/Middleware/Authenticate.php):

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Illuminate\Http\Exceptions\HttpResponseException;
use Closure;

class Authenticate
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    public function handle(Request $request, Closure $next)
    {
        if (!$request->expectsJson()) {
            throw new HttpResponseException(response()->json(['error' => 'Unauthenticated (middleware)'], 401));
        }

        return $next($request);
    }
}

Kernel.php:

protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
 	...
]

api.php

Route::middleware('auth')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('register', 'AuthController@register');
Route::post('login', 'AuthController@login');
Route::post('user/validate', 'UserController@validateUser');
Route::post('password/request', 'PasswordController@request');
Route::post('password/reset', 'PasswordController@reset');
Route::post('contact', 'ContactController@send');

// TODO remove this
Route::post('login/password', 'LoginController@password');

// User
Route::get('user', 'UserController@getUser')->middleware(['auth']);
Route::post('user', 'UserController@save')->middleware(['auth']);
Route::get('user/remove/request', 'UserController@removeRequest')->middleware(['auth']);
Route::post('user/remove', 'UserController@remove')->middleware(['auth']);

// Wishlist
Route::get('wishlist', 'WishlistController@all')->middleware(['auth']);
Route::get('wishlist/{hash}', 'WishlistController@getByHash')->middleware(['auth']);
Route::post('wishlist/{hash}/login', 'WishlistController@login');
Route::get('wishlist/user/{hash}', 'WishlistController@getOwnByHash')->middleware(['auth']);
Route::put('wishlist/{id}', 'WishlistController@update')->middleware(['auth']);
Route::post('wishlist', 'WishlistController@insert')->middleware(['auth']);
Route::delete('wishlist/{id}', 'WishlistController@remove')->middleware(['auth']);

// Wish
Route::post('wish', 'WishController@insert')->middleware(['auth']);
Route::put('wish/{id}', 'WishController@update')->middleware(['auth']);
Route::delete('wish/{id}', 'WishController@delete')->middleware(['auth']);

// Donate
Route::post('donate', 'DonateController@insert');
Route::get('donate/export/{hash}', 'DonateController@export')->middleware(['auth:api']);
rniederer's avatar

I fixed my problem now.

I had a custom middleware for authorization. I removed that now.

I get my route param like this:

$request->route()->parameter('hash');

Now it works. But I'm confused why it work locally and not work on webhosting...

rniederer's avatar

I know, but I didn't know how.

Normally it works like this:

[code]$request->route()->parameter->hash('hash')[/code]?

Or this:

$request->route()->parameter->hash('hash')?

But now i see the link to github...

$request->route()->parameter->hash('hash')

Next time...

Please or to participate in this conversation.