我的POST请求转到302 Found并调用GET

时间:2018-01-09 18:24:50

标签: php laravel .htaccess permissions apache2

我想设置 AWS Apache2 PHP7 Laravel 5.3

当我尝试POST/login时,它会将我重定向到GET /login

路由中已启用

POST /login,它位于列表中......但它无法正常工作。

你能帮我找一个知道在哪里看到一个bug吗?请。

形式:

    <form class="form-horizontal" role="form" method="POST" action="http://site.amazonaws.com/login">
<input type="hidden" name="_token" value="46ByPLZp03qEQqmdy6cq77DVe1xK837vcPRcrU4z">
    <div class="form-group">
        <label for="email" class="col-md-4 control-label">E-Mail Address</label>
        <div class="col-md-6">
            <input id="email" type="email" class="form-control" name="email" value="powlowski.moriah@example.com" required="" autofocus="">
        </div>
    </div>
    <div class="form-group">
        <label for="password" class="col-md-4 control-label">Password</label>
        <div class="col-md-6">
            <input id="password" type="password" class="form-control" name="password" required="">
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-6 col-md-offset-4">
            <div class="checkbox">
                <label><input type="checkbox" name="remember"> Remember Me</label>
             </div>
        </div>
    </div>

                    <div class="form-group">
                        <div class="col-md-8 col-md-offset-4">
                            <button type="submit" class="btn btn-primary">
                                Login
                            </button>  
                        </div>
                    </div>
                </form>
公共目录中的

.htaccess文件:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Apache2 conf 文件:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/lara-art/public
    <Directory /var/www/lara-art/public>
        Options Indexes FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

LoginController.php:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = '/artworks';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}

protected function credentials(Request $request)
{
    $field = filter_var($request->input($this->username()), FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
    $request->merge([$field => $request->input($this->username())]);
    return $request->only($field, 'password');
}
protected function attemptLogin(Request $request)
{
    return $this->guard()->attempt(
        $this->credentials($request), $request->has('remember')
    ) &&
    \Auth::user()->type->name!='art_lover';
}
protected function sendFailedLoginResponse(Request $request)
{
    \Auth::logout();
    return redirect()->back()
        ->withInput($request->only($this->username(), 'remember'))
        ->withErrors([
            $this->username() => \Lang::get('auth.failed'),
        ]);
}

public function username()
{
    return 'login';
}
}

1 个答案:

答案 0 :(得分:1)

Laravel 使用Auth::routes();

使用GET请求 laravel 返回视图, 但POST请求 laravel 检查您的登录数据是否正确... 如果您的登录名和密码不正确redirect则有错误; 你可以在视图中看到laravel验证错误;

示例:

@if($errors->any())

      @foreach($errors->all() as $error)
        <p>{{ $error }}</p><br/>
      @endforeach

@endif

或者您可以在登录控制器中使用此方法

public function login(Request $request)
{

    $this->validate($request, [
        'email' => 'required',
        'password'   => 'required|min:6',
    ]);


   if(Auth::attempt(['email' => $request->email, 'password' => $request->password])){

            return redirect(base_url('/'));
    }

    return redirect()->back()->withInput()->withErrors(['email'=> 'error message']);
}

我很抱歉我的英语))