laravel form post issue

后端 未结 7 1358
旧时难觅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: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')
    
    
    @if (isset($message))

    {{$message}}

    @endif {{ Form::open(array('url' => '/', 'method' => 'post')) }} {{ Form::text('url') }} {{ Form::text('valid') }} {{ Form::submit('shorten') }} {{ Form::close() }}
    @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');          
    });
    

提交回复
热议问题