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

Akeno's avatar

Route with parameter gets called twice

Hi,

I asked this in another forum as well but no one could help me. I have a very strange problem:

I have a route with a parameter:

Route::get('/develop/building/{building_id}', ['as' => 'develop.building', function($building_id)
    {
        // start developing
        ActionsController::create(Auth::user()->id, Session::get('location_id'), 1,Session::get('object_id'), $building_id, Carbon::now(), 1);
        return Redirect::route('overview');
    }]);

This route just adds a new entry to the database. Just one. Afterwards, it redirects to the overview-page.

I call this route with a link, that contains the id of the building I want to develop/upgrade.

I I use this generated link, everything works fine. It creates one new entry and redirects.

But when I enter the URL manually, it creates more than one entry. It differs from 2 to 5 entries. I have no idea why. Only one of those 2-5 entries contain the correct data. The rest contains other $building_id's that I entered recently.

I found this question at stack overflow. It seems that this is the exact same problem. But I don't know how he solved it: http://stackoverflow.com/questions/23422703/laravel-4-routes-with-parameters-gets-called-twice/23500953#23500953

Do you know what I could try? I run a local Apache server with MAMP on Mac OS X

0 likes
22 replies
bashy's avatar

Tested it on something like Postman? http://www.getpostman.com

Also, try return the data that is sent and maybe timing. If they're one after another, it could mean your connection or browser is sending multiple requests.

Other than that, it could be a number of things. Tested on different connections?

Akeno's avatar

Hm, no, I didn't try Postman. I didn't know that. I tried it but I have no idea how to log in. I used this "Basic Authentication" but it didn't work. Do you know how to do that?

Yes, I kind of tried that as well. The problem is that I can't just output every SQL query or something. That does not show up. I think the page reloads several times. I found out that if I remove the "return Redirect::route('overview');" it just creates one entry. But if I then go to the overview page it creates more entries.

It's difficult to explain, I completely don't understand the problem and what it causes.

yes, I tried it on different connections.

bashy's avatar

Sounds like you have some issues with your code maybe. If you do a return somewhere before the redirect one, it will stop execution after that. Try returning the data or maybe a foreach to see what gets sent.

Also check the network tab on dev tools in your browser, see what requests it's doing.

Akeno's avatar

I am totally confused right now o.O Everytime I record the network requests with safari developer tools, the problem doesn't appear and only one entry is created. But if I stop the recording, the problem starts again:

I deleted the Redirect and replaced it just with "return true;". Hence, I get a blank page when I enter the URL "/public/develop/building/5". Then I look into the database and I see only one entry. That's correct. Then I manually return to "/public/overview" and suddenly a new entry appears in the database.

I know for sure that this is caused by the Code in my Route I posted (where I call the ActionsController) because I tried to delete it before I returned to the overview page (then, no new entry is created).

So, somehow laravel calls this route even if another route is requested. Does it maybe has something to do with my routes in general? Because I use a group of routes:

Route::group(['before' => 'auth'], function()
{
   Route::get('/overview', ['as' => 'overview', 'uses' => 'PagesController@showOverview']);
    Route::get('/develop/building/{building_id}', ['as' => 'develop.building', function($building_id)
    {
        // start developing

       ActionsController::create(Auth::user()->id, Session::get('location_id'), 1,Session::get('object_id'), $building_id, Carbon::now()->addSeconds(5), 1);
        return true;
    }]);
});
bashy's avatar

Tried directing it to a controller instead of using a closure?

Also tried using cURL via cli to get the page without a browser?

1 like
Akeno's avatar
  1. hm, now there are always just 2 entries (one of them is correct). And if I don't redirect to the overview page it still works correctly.

  2. With this, nothing is added to the database. Very strange :/

bashy's avatar

Not sure what else to suggest, hard when I'm not actually there checking things and narrowing it down :P

Akeno's avatar

Yeah, that's difficult, I know^^ But thank you very much for trying :)

Maybe I could upload the project files somewhere so you can download them. If you want.

Akeno's avatar

not much :/

 public function showOverview()
    {
        $object = Object::with('buildings.building_type')->findOrFail(Session::get('object_id'));
        $buildings = $object->buildings;

        return View::make('pages.overview', compact('buildings'));
    }
sitesense's avatar

Hmm... if you refresh the overview page, does it create further records?

Akeno's avatar

only if I opened "develop/building/{someNumber}" before. If I reload a second or third time nothing gets created in the database

1 like
sitesense's avatar

Clutching at straws here but do you have any images with an empty "src" tag, or perhaps a css style attribute that has an empty url like:

background-image: url();

These things could cause further requests to the same page.

Akeno's avatar

For a second, I thought that would be the answer :D Because I actually had this in the code but it didn't solve the problem :(

sitesense's avatar

Too bad... It might sound obvious but have you tried a different browser? Sometimes plugins/extensions can cause unexpected behaviour.

If that doesn't work I think you'll have to post your code somewhere.

Good luck :D

Akeno's avatar

hm, normally I use safari. Now I tried with Chrome. I had no problems. It created just one entry like it should do.

Afterwards, I deactivated the extensions in safari. But the problem is still there :/ I noticed that the first time (after opening safari) it works correctly. From the second try on I experience this problem, although I deactivated caching. It's very strange

sitesense's avatar

Is Safari up to date? It may be a bug that has been fixed.

Akeno's avatar

hm, It tried it with several browsers now. The all work fine except for safari :/

So I think we can assume it's a bug in safari in combination with laravel. Hm, not perfect, but at least I now the reason for my problem now.

Thank you both very much :)

mikebarwick's avatar

Dude...currently experiencing the same thing with safari. Did you ever solve - 3+ years ago?? lol

ElectricPutty's avatar

Perhaps check your Middlewear.

I had this situation and found one of my Middlewears looked like this:

    $response = $next($request);
    $response->headers->set('P3P', 'CP="NOI ADM DEV COM NAV OUR STP"');
    return $next($request);

The two calls to $next($request) meant the route was being hit twice.

I changed this to:

    $response = $next($request);
    $response->headers->set('P3P', 'CP="NOI ADM DEV COM NAV OUR STP"');
    return $response;

And it no longer happens.

sihendra's avatar

@ElectricPutty thanks! this fixed my problem.

Btw in my case I called the parent handle method.

public function handle($request, Closure $next, ...$guards)
{
        parent::handle($request, $next, $guards);
...
}

Please or to participate in this conversation.