0

我是 Laravel4 世界的新手,我正在尝试将我的网站转换为 laravel 框架。但一开始我有一个小问题。在索引上,我有两个 jquery 模式:第一个用于唱歌,第二个用于注册。所以,我想当用户尝试登录并出错时向他显示错误。注册也一样。但是当我在唱歌表格中出现错误时,我会收到注册表......我的英语不好,所以这里是代码:

路由.php

Route::get('/', function()
{
     return View::make("index.index");
}
Route::post('/', array('as'=> 'singin', function()
{
    $data = Input::all();
    $rules = array(
        'username' => 'min:3',
        'password' => 'min:6'
    );
    $validation = Validator::make($data, $rules);
    if($validation->passes())
    {
        return View::make('users.main');
    }
    Input::flash();

    return Redirect::to('/')->withErrors($validation);
}));
Route::post('/', array('as'=> 'register', function()
{
    // same as previous function
]

index.blade.php

<div id="signin" class="reveal-modal" @if($errors->all())
style="display:block; visibility:visible;" @endif >
    <header class="reveal-modal-header">
        Sign in
    </header>
    <div class="cont">

        <div class="indication">
            Sign In
        </div>
        {{ Form::open(array(
            'route' => 'singin',
            'class' => 'signin')) }}
        <p class="clearfix">
            {{ Form::label('username', 'Username') }}
            {{ Form::text('username')}}
        </p>
        <p class="clearfix">

            {{ Form::label('password', 'Password') }}
            {{ Form::password('password')}}
        </p>
        <p class="without-label clearfix">
            {{ Form::submit('') }}
        </p>
        @if($errors->all())
        <div id="logindiverror" style="" class="alert alert-error">
        @foreach($errors->all('<li>:message</li>') as $message)
         {{ $message }}
        @endforeach
        </div>
        @endif

        {{ Form::close() }}
    </div>
    <a class="close-reveal-modal" id="close1" href="#">&#215;</a>
</div>

<div id="signup" class="reveal-modal" @if($errors->all())
style="display:block; visibility:visible;" @endif >>
    <header class="reveal-modal-header">
        Sign up
    </header>
    <div class="cont">
        <div class="indication">
            Register
        </div>
        {{ Form::open(array(
            'route' => 'register',
            'class' => 'signup')) }}

            <p class="clearfix">
                <em>(<abbr>*</abbr>) All fields are required</em>
            </p>
            <p class="clearfix">
                <label>Username:<abbr>*</abbr></label>
                {{ Form::text('username')}}
            </p>
            <p class="clearfix">
                <label>Email:<abbr>*</abbr></label>
                {{ Form::text('email') }}
            </p>
            <p class="clearfix">
                <label>Password:<abbr>*</abbr></label>
                {{ Form::password('password1'); }}
            </p>
            <p class="clearfix">
                <label>Password (repeat):<abbr>*</abbr></label>
                {{ Form::password('password2'); }}
            </p>

            <p class="checkboxes clearfix">
                <span class="niceCheck">{{ Form::checkbox('agree', '0') }}</span>
                I agree with LinkyPlanet Terms &amp; Privacy Policy
            </p>
        <p class="without-label clearfix">
            {{ Form::submit('') }}
        </p>

        @if($errors->all())
        <div id="logindiverror" style="" class="alert alert-error">
            @foreach($errors->all('<li>:message</li>') as $message)
            {{ $message }}
            @endforeach
        </div>
        @endif

        {{ Form::close() }}

    </div>
    <a class="close-reveal-modal" id="close2" href="#">&#215;</a>
</div>

所以,问题是当用户尝试唱歌时,他输入了密码错误(密码短于 6 个字符),脚本将他重定向到索引并打开模式进行注册,并且显示错误,而不是在模式中唱歌。错在哪里?

干杯:)

4

1 回答 1

1

问题是您有 2 个“不同”的路由使用相同的 URI ( /) 和相同的方法 ( POST),但您不能。

只有第一个会优先工作。这样的事情可能会更好:

Route::post('/singin', array('as'=> 'singin', function()
{

}));

Route::post('/register', array('as'=> 'register', function()
{

}));

或者您可以保留其中一个/并更改另一个:

Route::post('/', array('as'=> 'singin', function()
{

}));

Route::post('/register', array('as'=> 'register', function()
{

}));

编辑:

您对所有内容都使用相同的名称(注册和登录中的 $errors)。如果两者都有一个视图,则需要使用不同的名称。您可以使用

return Redirect::to('/')->with('registerErrors', $validation->messages());

return Redirect::to('/')->with('signInErrors', $validation->messages());

但是在 tedirects 上,它们绑定到您的会话,因此您必须以这种方式获取它们:

@if(Session::has('registerErrors'))
    <div id="logindiverror" style="" class="alert alert-error">
        @foreach(Session::get('registerErrors')->all('<li>:message</li>') as $message)
            {{ $message }}
        @endforeach
    </div>
@endif
于 2013-11-10T21:45:52.203 回答