0

My problem: when I do GET /print-jobs/, it correctly routes to the index method in my controller. When I say POST /print-jobs/, it should route to my store method. However, it seems to be routing to my index method as well. This only happens in my production environment, where I am running Laravel under Apache.

Route code:

Route::group(['prefix' => 'print-jobs'], function () {
    Route::get('/', 'PrintJobMasterController@index');
    Route::get('/{printJob}', 'PrintJobMasterController@show')->where(['printJob' => '[0-9]+']);
    Route::post('/', 'PrintJobMasterController@store');
    ...
 }

Relevant controller code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class PrintJobMasterController extends Controller {
    /**
     * Display all PrintJob models
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
        Log::info('PJMC::index: rendering index of all print jobs');
        ...;
    }
    /**
     * Store a newly job request en masse
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        Log::info("PJMC::store: creating new job");
        ...;
    }
    ...;
}

Now, I expect to see in my logs a message starting with PJMC::index: when I call GET /print-jobs/ and a message starting with PJMC::store: when I call POST /print-jobs/. However, both times when do this in the browser: $.get('/print-jobs') and $.post('/print-jobs/', {_token: window.Laravel.csrfToken, ...}) from my browser, I see the following in my log file:

[2017-06-09 10:27:17] local.INFO: PJMC::index: rendering index of all print jobs
[2017-06-09 10:27:23] local.INFO: PJMC::index: rendering index of all print jobs

Both times the request routes to the index method! I looked in my network history on my browser and the requests are indeed registered as a GET and a POST respectively, which leads me to suspect that the problem is somewhere on the backend. However, when I call these routes using Laravel's test suite, I don't have any problems.

Any ideas? Thanks in advance.

Ashton Wiersdorf
  • 1,865
  • 12
  • 33

1 Answers1

1

Remove the trailing slash / in your $.post() and it should work.

Wrong:

$.post('/print-jobs/', {_token: window.Laravel.csrfToken, ...})

Correct:

$.post('/print-jobs', {_token: window.Laravel.csrfToken, ...})
baikho
  • 5,203
  • 4
  • 40
  • 47
  • I think that did it. Thanks! Why does this make a difference? Should I avoid trailing slashes in URIs in general? Are there cases when they are appropriate/necessary and when they are not? – Ashton Wiersdorf Jun 09 '17 at 22:35
  • 1
    When using Laravel yes. There's a redirect rule in the `.htaccess` file. Have a look at [Laravel trailing slashes redirect to localhost](https://stackoverflow.com/questions/21735527/laravel-trailing-slashes-redirect-to-localhost) – baikho Jun 10 '17 at 23:21