In the frontendRoutes.php the "get" Method does work, the "post" Method does not work. I get the error "MethodNotAllowedHttpException in RouteCollection.php line 219:"



  • in the frontendRoutes.php the "get" Method does work, the "post" Method does not work. I get the error
    MethodNotAllowedHttpException in RouteCollection.php line 219:

    if i will send a contact form with method "post"

    here is my route in frontentRoutes.php
    post('contacts/formular', ['as' => $locale . '.newsletter.kontaktform', 'uses' => 'PublicController@kontaktform']);

    here is my route in the form
    {!! Form::open(array('url' => 'newsletter/contacts/formular', 'method' => 'post')) !!}

    <!-- Content from my frontentRoutes.php -->

    use Illuminate\Routing\Router;
    /** @var Router $router */
    
    $router->group(['prefix' =>'/newsletter'], function (Router $router) {
        $locale = LaravelLocalization::setLocale() ?: App::getLocale();
        $router->bind('contacts', function ($id) {
            return app('Modules\Newsletter\Repositories\ContactRepository')->find($id);
        });
    
        get('posts', ['as' => $locale . '.newsletter', 'uses' => 'PublicController0']);
        post('contacts/anmelden', ['as' => $locale . '.newsletter', 'uses'  => 'PublicController@anmelden']);
        post('contacts/formular', ['as' => $locale . '.newsletter.kontaktform', 'uses'  => 'PublicController@kontaktform']);
        get('freischalten/{key}', ['as' => $locale . '.newsletter', 'uses'  => 'PublicController@freischalten']);
        get('abmelden/{key}', ['as' => $locale . '.newsletter', 'uses'  => 'PublicController@abmelden']);
        get('schema/{status?}/{table?}', ['as' => $locale . '.newsletter', 'uses'  => 'PublicController@schemaBuilder']);
    
    });
    
    <!-- Mailform in kontakt.blade.php -->
    {!! Form::open(array('url' => 'newsletter/contacts/formular', 'method' => 'post')) !!}
    {!! Form::label('email', 'E-Mail Address', array('class' => 'awesome')) !!}
    {!! Form::text('email', 'example@gmail.com') !!}
    {!! Form::submit('Click Me!') !!}
    {!! Form::close() !!}
     <!-- END mailform -->
    

    Many thanks for your help
    best regards
    Didi


  • Global Moderator

    @dieter

    Hey, you don't need to explicitly create named route for each locale.
    In form you are sending to different route than you created or is your routes under group?
    Cant really see.

    Also you need CSRF token if you haven't disabled it.



  • @armababy

    Thank you for your help.
    Unfortunately I do not know how such a token works. Is that correct in form:

    <input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />

    Or if you use the form builder:

    {!! Form::token() !!}

    Help??
    what to a value in value="{{ Session::token() }}"

    Thank you


  • Global Moderator

    @dieter

    You can check out all options of csrf over at laravel documentation
    But basically you have 2 options either <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> or {{ csrf_field() }} from the view.

    But it seems LaravelCollective does that for you already if you specify method explicitly https://laravelcollective.com/docs/5.1/html#csrf-protection

    Anyhow MethodNotAllowedHttpException is not because of token, if it would be token then usually you get TokenMissmatchException, so that would suggest you have wrong route setup or calling of them, as exception tells us we use wrong method for the url (2 things here, does not exist or expect other method).

    Could you post what you get from console when you do php artisan route:list --path=newsletter from your asgard root folder.



  • Thank you for the help :)
    Hier are my data

    +--------+----------+-----------------------------------------------+---------------------------------------+--------------------------------------------------------------------------+---------------------------------------------+
    | Domain | Method   | URI                                           | Name                                  | Action                                                                   | Middleware                                  |
    +--------+----------+-----------------------------------------------+---------------------------------------+--------------------------------------------------------------------------+---------------------------------------------+
    |        | GET|HEAD | backend/newsletter/blacklists                 | admin.newsletter.blacklist.index      | Modules\Newsletter\Http\Controllers\Admin\BlacklistController4      | localizationRedirect,auth.admin,permissions |
    |        | POST     | backend/newsletter/blacklists                 | admin.newsletter.blacklist.store      | Modules\Newsletter\Http\Controllers\Admin\BlacklistController@store      | localizationRedirect,auth.admin,permissions |
    |        | POST     | backend/newsletter/blacklists/create          | admin.newsletter.blacklist.create     | Modules\Newsletter\Http\Controllers\Admin\BlacklistController@create     | localizationRedirect,auth.admin,permissions |
    |        | DELETE   | backend/newsletter/blacklists/{contacts}      | admin.newsletter.blacklist.destroy    | Modules\Newsletter\Http\Controllers\Admin\BlacklistController@destroy    | localizationRedirect,auth.admin,permissions |
    |        | PUT      | backend/newsletter/blacklists/{contacts}/edit | admin.newsletter.blacklist.update     | Modules\Newsletter\Http\Controllers\Admin\BlacklistController@update     | localizationRedirect,auth.admin,permissions |
    |        | GET|HEAD | backend/newsletter/blacklists/{contacts}/edit | admin.newsletter.blacklist.edit       | Modules\Newsletter\Http\Controllers\Admin\BlacklistController@edit       | localizationRedirect,auth.admin,permissions |
    |        | POST     | backend/newsletter/contacts                   | admin.newsletter.contact.store        | Modules\Newsletter\Http\Controllers\Admin\ContactController@store        | localizationRedirect,auth.admin,permissions |
    |        | GET|HEAD | backend/newsletter/contacts                   | admin.newsletter.contact.index        | Modules\Newsletter\Http\Controllers\Admin\ContactController4        | localizationRedirect,auth.admin,permissions |
    |        | GET|HEAD | backend/newsletter/contacts/create            | admin.newsletter.contact.create       | Modules\Newsletter\Http\Controllers\Admin\ContactController@create       | localizationRedirect,auth.admin,permissions |
    |        | GET|HEAD | backend/newsletter/contacts/listen            | admin.newsletter.contact.listen       | Modules\Newsletter\Http\Controllers\Admin\ContactController@listen       | localizationRedirect,auth.admin,permissions |
    |        | DELETE   | backend/newsletter/contacts/{contacts}        | admin.newsletter.contact.destroy      | Modules\Newsletter\Http\Controllers\Admin\ContactController@destroy      | localizationRedirect,auth.admin,permissions |
    |        | GET|HEAD | backend/newsletter/contacts/{contacts}/edit   | admin.newsletter.contact.edit         | Modules\Newsletter\Http\Controllers\Admin\ContactController@edit         | localizationRedirect,auth.admin,permissions |
    |        | PUT      | backend/newsletter/contacts/{contacts}/edit   | admin.newsletter.contact.update       | Modules\Newsletter\Http\Controllers\Admin\ContactController@update       | localizationRedirect,auth.admin,permissions |
    |        | GET|HEAD | backend/newsletter/inhalts                    | admin.newsletter.inhalt.index         | Modules\Newsletter\Http\Controllers\Admin\InhaltController4         | localizationRedirect,auth.admin,permissions |
    |        | POST     | backend/newsletter/inhalts/create             | admin.newsletter.inhalt.create        | Modules\Newsletter\Http\Controllers\Admin\InhaltController@create        | localizationRedirect,auth.admin,permissions |
    |        | POST     | backend/newsletter/inhalts/store2             | admin.newsletter.inhalt.store2        | Modules\Newsletter\Http\Controllers\Admin\InhaltController@store2        | localizationRedirect,auth.admin,permissions |
    |        | GET|HEAD | backend/newsletter/inhalts/vorschau           | admin.newsletter.inhalt.vorschau      | Modules\Newsletter\Http\Controllers\Admin\InhaltController@vorschau      | localizationRedirect,auth.admin,permissions |
    |        | DELETE   | backend/newsletter/inhalts/{contacts}         | admin.newsletter.inhalt.destroy       | Modules\Newsletter\Http\Controllers\Admin\InhaltController@destroy       | localizationRedirect,auth.admin,permissions |
    |        | GET|HEAD | backend/newsletter/inhalts/{contacts}/edit    | admin.newsletter.inhalt.edit          | Modules\Newsletter\Http\Controllers\Admin\InhaltController@edit          | localizationRedirect,auth.admin,permissions |
    |        | PUT      | backend/newsletter/inhalts/{contacts}/edit    | admin.newsletter.inhalt.update        | Modules\Newsletter\Http\Controllers\Admin\InhaltController@update        | localizationRedirect,auth.admin,permissions |
    |        | GET|HEAD | backend/newsletter/versands                   | admin.newsletter.versand.index        | Modules\Newsletter\Http\Controllers\Admin\VersandController4        | localizationRedirect,auth.admin,permissions |
    |        | POST     | backend/newsletter/versands/create            | admin.newsletter.versand.create       | Modules\Newsletter\Http\Controllers\Admin\VersandController@create       | localizationRedirect,auth.admin,permissions |
    |        | GET|HEAD | backend/newsletter/versands/emailversand      | admin.newsletter.versand.emailversand | Modules\Newsletter\Http\Controllers\Admin\VersandController@emailversand | localizationRedirect,auth.admin,permissions |
    |        | POST     | backend/newsletter/versands/store2            | admin.newsletter.versand.store2       | Modules\Newsletter\Http\Controllers\Admin\VersandController@store2       | localizationRedirect,auth.admin,permissions |
    |        | DELETE   | backend/newsletter/versands/{contacts}        | admin.newsletter.versand.destroy      | Modules\Newsletter\Http\Controllers\Admin\VersandController@destroy      | localizationRedirect,auth.admin,permissions |
    |        | GET|HEAD | backend/newsletter/versands/{contacts}/edit   | admin.newsletter.versand.edit         | Modules\Newsletter\Http\Controllers\Admin\VersandController@edit         | localizationRedirect,auth.admin,permissions |
    |        | PUT      | backend/newsletter/versands/{contacts}/edit   | admin.newsletter.versand.update       | Modules\Newsletter\Http\Controllers\Admin\VersandController@update       | localizationRedirect,auth.admin,permissions |
    |        | GET|HEAD | newsletter/abmelden/{key}                     | de.newsletter                         | Modules\Newsletter\Http\Controllers\PublicController@abmelden            | localizationRedirect                        |
    |        | POST     | newsletter/contacts/anmelden                  | de.newsletter                         | Modules\Newsletter\Http\Controllers\PublicController@anmelden            | localizationRedirect                        |
    |        | GET|HEAD | newsletter/contacts/formular                  | de.newsletter.kontaktform             | Modules\Newsletter\Http\Controllers\PublicController@kontaktform         | localizationRedirect                        |
    |        | GET|HEAD | newsletter/freischalten/{key}                 | de.newsletter                         | Modules\Newsletter\Http\Controllers\PublicController@freischalten        | localizationRedirect                        |
    |        | GET|HEAD | newsletter/posts                              | de.newsletter                         | Modules\Newsletter\Http\Controllers\PublicController4               | localizationRedirect                        |
    |        | GET|HEAD | newsletter/schema/{status?}/{table?}          | de.newsletter                         | Modules\Newsletter\Http\Controllers\PublicController@schemaBuilder       | localizationRedirect                        |
    +--------+----------+-----------------------------------------------+---------------------------------------+--------------------------------------------------------------------------+---------------------------------------------+
    

  • Global Moderator

    @dieter

    Okay,
    Starting off we can see that you have newsletter/contacts/formular route that accepts GET|HEAD but you are trying to POST data to it.
    Change this route from get to post in routes and try again then we can continue.



  • Unfortunately, there is not still. This is the error:

    MethodNotAllowedHttpException in RouteCollection.php line 219:

    The Route is set:
    | POST | newsletter/contacts/formular | de.newsletter.kontaktform | Modules\Newsletter\Http\Controllers\PublicController@kontaktform | localizationRedirect

    The Form i have set the _doken

    {!! Form::open(array('url' => 'newsletter/contacts/formular', 'method' => 'post')) !!}
    <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

    token even this does not work:
    <input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />

    Thanks for your help.
    Dieter


  • Global Moderator

    @dieter

    So i copied parts of your code to try and replicate your problem.
    I could not replicate it in the end and everything is working fine, but i have some information on this issue.

    • Use named routes instead of direct routing in form calls to avoid multilanguage confusion
      instead of
      {!! Form::open(array('url' => 'newsletter/contacts/formular', 'method' => 'post')) !!}
      use
      {!! Form::open(array('route' => 'newsletter.formular', 'method' => 'post')) !!}
      named routes are good and will save you lots of time in the future if you change route structure for example.

    • When registering routes you don't need prepend locale so
      instead of
      post('contacts/formular', ['as' => $locale . '.newsletter.kontaktform', 'uses' => 'PublicController@kontaktform']);
      do it simply like this
      post('contacts/formular', ['as' => 'newsletter.kontaktform', 'uses' => 'PublicController@kontaktform']);
      again no need to put locale there, you can always retrieve locale on request in your controller.

    Also as i mentioned do not worry about token as it is included by default for all POST, GET, PUT routes by LaravelCollective package.


  • Global Moderator

    Marked as answered.
    Due to no response from OP on potential/actual solution proposal for over 1 month.

    If OP or someone thinks this is not still solved, please respond with problem/concern.


Log in to reply