laravel形式不按预期指示

时间:2015-11-13 07:16:35

标签: php laravel

我正在laravel创建一个注册。

我希望提交此表单带我(我所在的同一页)http://localhost:8888/mylaravel/notetomyself/public/auth/register

但提交时实际上最终会在http://localhost:8888/auth/register

以下代码是我在views / auth / register.blade.php

中的视图
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Register</div>
                <div class="panel-body">

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

<form class="form-horizontal" role="form" method="POST" action="/auth/register">
<input type="hidden" name="_token" value="{{ csrf_token() }}">

<div class="form-group">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="name" value="{{ old('name') }}">
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password_confirmation">
</div>
</div>

<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
    Register
</button>
</div>

</div>
</form>
</div>
</div>
</div>
</div>
</div>

我的routes.php文件包含:

Route::get('login', array('uses' => 'HomeController@showLogin'));
Route::get('home', array('uses' => 'Home@index'));
Route::post('login', array('uses' => 'HomeController@doLogin'));
Route::get('logout', array('uses' => 'HomeController@doLogout'));
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
Route::controllers([
   'password' => 'Auth\PasswordController',
]);

控制器auth / AuthController.php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    private $redirectTo = '/';
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

想知道是否有某个地方我可能设置了错误的路径?

2 个答案:

答案 0 :(得分:1)

为什么要重定向到那里:

因为你形成了/auth/register的行动点[因为没有完整路径的网络服务器只是将你的域名视为根路径]。

你应该做什么:

只需移除/

auth之前的action="/auth/register"即可

其他提示:(修复后要做)

要在身份验证之后/之前处理重定向,您应该根据Authenticate.php中的RedirectIfAuthenticated.phpapp/Http/Middleware return redirect()->guest('yourpath');内的return $next($request);进行自定义,或者在请求中{ {1}}

注意:

根据您的路线更改动作!

答案 1 :(得分:1)

正如其他人所说,你的路径是相对于根域的。最简单的解决方案是使用Laravel的URL帮助程序函数来创建绝对URL。你有几个选择。

  1. 生成指定路径的绝对URL:

    action="{{ url('auth/register') }}"
    
  2. 使用路径名称生成绝对URL

    action="{{ route('routeName') }}"
    
  3. 生成指定控制器操作的绝对URL:

    action="{{ action('Auth\AuthController@postRegister') }}"
    
  4. 来源:http://laravel.com/docs/5.1/helpers#urls