laravel form post issue

后端 未结 7 1296
旧时难觅i
旧时难觅i 2021-01-05 19:04

I am building a practice app with the Laravel framework I built a form in one of the views which is set to post to the same view itself but when I hit

相关标签:
7条回答
  • 2021-01-05 19:43
    {{ Form::open(array('url' => ' ', 'method' => 'post')) }}
    

    Passing a space as the url has worked for me.

    0 讨论(0)
  • 2021-01-05 19:44

    The problem is with defualt slashed in Apache from 2.0.51 and heigher: http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryslash

    The best solution if you do not want to change Apache config is to make the post in a different path:

    GOOD:

    Route::get('/', 
      ['as' => 'wizard', 'uses' => 'WizardController@create']);
    
    Route::post('wizard-post', 
      ['as' => 'wizard_store', 'uses' => 'WizardController@store']);
    

    NOT GOOD:

    Route::get('/', 
      ['as' => 'wizard', 'uses' => 'WizardController@create']);
    
    Route::post('/', 
      ['as' => 'wizard_store', 'uses' => 'WizardController@store']);
    
    0 讨论(0)
  • 2021-01-05 19:45

    I have same problem with OSx + MAMP, initially I've resolved with Raul's solution:

    {{ Form::open(array('url' => ' ', 'method' => 'post')) }}
    

    but after consultation with my friend we have concluded that my problem was due to the fact my lavarel project is avaliable by long local path, as:

     http://localhost/custom/custom2/...
    

    in this location the post/get method on root path ("/") not working correctly.

    Lavarel to working correctly must be avaliable by "vhost", in this case the problem get/post method on root location "/" not exist.

    My friend advised me to use http://www.vagrantup.com/

    BYE

    0 讨论(0)
  • 2021-01-05 19:47

    There is some helpfull information in the Laravel Docs. Check these out:

    • Resource Controllers (or RESTful Controllers)
    • Forms & HTML
      • Opening A Form
    • Routing
      • Named Routes

    I recommend you read the Resource Controllers documentation as it makes form handling a lot easier.

    0 讨论(0)
  • 2021-01-05 19:56

    Well, you just return the view, so nothing change. You should bind your route to a controller to do some logic and add data to your view, like this:

    index.blade.php

    @extends('master')
    
    @section('container')
    
    <div class="wrapper">
         @if (isset($message))
         <p>{{$message}}</p>
         @endif
    
        {{ Form::open(array('url' => '/', 'method' => 'post')) }}
            {{ Form::text('url') }}
            {{ Form::text('valid') }}
            {{ Form::submit('shorten') }}
        {{ Form::close() }}
    
    </div><!-- /wrapper -->
    
    @stop
    

    Your routes

    Routes::any('/', 'home@index');
    

    You controller HomeController.php

    public function index()
    {
         $data = array();
         $url = Input::get('url');
         if ($url)
              $data['message'] = "foo";
    
         return View::make('index', $data);
    }
    

    You can also modify your current routes without using a controller like this (use the new view file)

    Route::get('/', function()
    {
         return View::make('index'); 
    });
    
    Route::post('/', function() 
    {
         return View::make('index')->with('message', 'Foo');          
    });
    
    0 讨论(0)
  • 2021-01-05 19:57

    You need to make sure that your form's method does NOT end in a / for it to be routed correctly. For example if you have the following route:

    Route::post('form/process', function()
    {
       # code here ...    
    });
    

    Then you need to have the following form definition:

    <form action="/form/process" method="POST">
    

    I hope that helps.

    0 讨论(0)
提交回复
热议问题