1
votes

I have a Laravel 5.2 Practice application which is running with Laravel Serve running like this Laravel 5.2 Practice

Working Perfectly , The form blade is following

@extends('layouts.app')

@section('form')
<div class="col-md-4">
@if(count($errors)>0)
<div class="alert alert-danger">
    @foreach($errors->all() as $error)
        {{ $error }}
    @endforeach
</div>
@endif
<form action="/" method="post">
    <div class="form-group">
        <label>I want to</label>
        <select class="form-control" name="action">
            <option value="greet">Greet</option>
            <option value="hug">Hug</option>
            <option value="kiss">Kiss</option>
        </select>
    </div>
    <div class="form-group">
        <label>this person</label>
        <input type="text" class="form-control" name="name" >
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-success "> Do it! </button>
    </div>
  </form>
 </div>
 @endsection

With Following POST route entry in routes

Route::post('/', 'adminController@post');

Problem

When I run the same application using Wamp server instead of Laravel Server

Laravel 5.2 Practice Application

I press submit button, it takes me to localhost

Wamp Server Localhost

Is it possible to write a route entry which can perform in both situations, with and without Laravel Server running. ?

These are my routes

Route::get('/', [
  'uses' => 'adminController@index',
  'as'  => 'home'
]);
Route::get('/hug/{name?}', [
  'uses' => 'adminController@hug',
  'as'  => 'hug'
]);
Route::get('/kiss/{name?}', [
  'uses' => 'adminController@kiss',
  'as'  => 'kiss'
]);
Route::get('/form', [
  'uses' => 'adminController@form',
  'as'  => 'form'
]);

Route::post('/', 'adminController@post')->name('index'); 


Route::auth();

Route::get('/home', 'adminController@index');
Route::get('/logout',function(){
Auth::logout();
 return Redirect::to('/home');
});
3
Route::post('/', 'adminController@post')->name('index'); and add in form action route('index')sunilwananje
what should I write in action of the form?Malik Mudassar
add {{ route('index') }} to form actionsunilwananje
@sunilwananje now it takes me to localhost/blog_new/public means again on root. I have edited the post, see my routes fileMalik Mudassar
on which page you want to redirect?sunilwananje

3 Answers

2
votes

This happens because you hard-coded the / in

<form action="/" method="post">

I highly recommend using Laravel Collective for your forms

You can open a form giving a route, not an absolute URL like this. Read the documentation.

echo Form::open(['route' => 'route.name'])

Laravel Collective will solve all your issues like this. It has model binding and CSRF protection. Easy to use.

Hope this help.

1
votes

If you don't want to use the form collections, in the form action, replace the action="/" to your route url:

<form action="your_post_url" method="post">
    <!-- protect your application from cross-site request forgery (CSRF) attacks. -->
    {{ csrf_field() }}
    <!-- your form here -->
</form

To find the route url, you can use the command php artisan route:listin your console

0
votes

Alright I have fixed it by doing following.

Route Entry

Route::post('/post', 'adminController@post')->name('post'); 

Form Open

<form action="{{ route('post') }}" method="post">

Previously the Route was pointing to /. I still have no idea why mentioning /post fixed it when I named the route to post, but its working now