dream2891 2015-06-26 17:40
浏览 63

Laravel表单POST重定向失败

I'm having a very frustrating (super-newbie) experience with Laravel 5.1. Sure there must be something that I'm missing, but unfortunately I couldn't find anything on Laravel docs.

The problem is this (and I believe is even relatively simple): while all GET routes are working, the routes in POST are 'rerouting' me to the wrong place. For instance, assuming that the controller is this:

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use View;

class PagesController extends BaseController {


  public function hello(){
    return view('hello');

  }




 public function register(){
    //Registration rules
    $inputData=array(
            'name' => \Input::get('name'),
             'email' =>\Input::get('email'),
             'password' => bcrypt(\Input::get('password')),
        );  


        createUser($inputData);

    return view('stat');
  }     




 private function createUser($inputData){
    return User::create($inputData);
  }

}

and given the route:

Route::get('/','PagesController@hello');

this one redirects me correctly to the view specified in the controller,at the method indicated.

A POST operation cannot be successfully performed, since

Route::post('/','PagesController@register');

with the form having:

<form action="/" method="post">
        <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
        <label for="name">User Name</label><br/>
        <input type="text" name="name" id="name"/><br/>
        <label for="name">E-mail</label><br/>
        <input type="text" name="email" id="email"/><br/>
        <label for="password">password</label><br/>
        <input type="password" name="password" id="password"/><br/>
        <input type="submit" value="auth!"/>
    </form>

results in redirecting me to xampp home page. For example, the "main registration page" is at this url:

http://localhost:8080/laravel/project1/public/

(GET OK) and when I click on the submit button, I'm sent to

http://localhost:8080/

Just few more indicators for you to try and help me (I'm genuinely stuck): 1) I didn't use a VirtualHost (searched on the web, but had always no luck in configuring apache properly...good luck I've got xampp) 2) The page "hello" has a link that I used as a test:

<a href="stat">Link</a>

and it redirects me correctly to the sought-after

http://localhost:8080/laravel/project1/public/stat

3)In the POST method, I've used a dd($inputData) to see what was going wrong, and as a result I had...nothing. Not a blank page,just always the localhost page. This led me to think that somehow the controller method isn't called, since no dd(---) result got in page. Hope someone can help. Many thanks

  • 写回答

2条回答 默认 最新

  • douluozhan4370 2015-06-26 17:45
    关注

    Your domain is http://localhost:8080 , so when you set action to '/' , it goes to root. It`s not good practice , but your solution is to change :

    <form action="/laravel/project1/public/" method="post">
    

    I`d create a virtual host for the project in apache , so document root would be /laravel/project1/public/ .

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题