gpt4 book ai didi

php - laravel 通过 Controller 将错误传递给 View

转载 作者:可可西里 更新时间:2023-10-31 22:11:39 25 4
gpt4 key购买 nike

我正在尝试在我的 Controller 中验证一个 Action 。一旦失败,我将验证器生成的消息发送到同一 Controller 中的其他函数,以将其传递给 View 。唯一的问题是我在 View 中看不到任何显示,并且一旦我单击提交并发生错误,url 就会更改为奇怪的格式。

Controller :

<?php

class MembersController extends BaseController {

protected $layout = 'layouts.master';


public function loadRegisterView($input = null)
{
if($input == null)
return View::make('members.register');
else
return View::make('members.register', $input);
}

public function loadLoginView()
{
return View::make('members.login');
}


public function register()
{

$rules = array(
'first_name' => 'Required|Min:3|Max:88|Alpha',
'last_name' => 'Required|Min:3|Max:88|Alpha',
'password' => 'Required|Min:3|Max:88|Alpha|Confirmed',
'email' => 'Required|email|unique:users',
);

$validator = Validator::make(Input::all(), $rules);


if ($validator->fails())
{
$messages = $validator->messages();
return Redirect::action('MembersController@loadRegisterView', $messages);
}else{
return "Thank you!";
}

}
}

路线:

Route::get('login', array('as' => 'login', 'uses' => 'MembersController@loadLoginView'));

Route::get('signup', array('as' => 'signup', 'uses' => 'MembersController@loadRegisterView'));


Route::get('/', 'MainController@index');


Route::post('processRegistration', array('as' => 'processRegistration', 'uses' => 'MembersController@register'));

在 View 中:

@if ( $errors->count() > 0 )
<p>The following errors have occurred:</p>

<ul>
@foreach( $errors->all() as $message )
<li>{{ $message }}</li>
@endforeach
</ul>
@endif

URL 如下所示:

http://localhost:8888/store/index.php/signup?%00%2A%00messages%5Bfirst_name%5D%5B0%5D=The+first+name+field+is+required.&%00%2A%00messages%5Blast_name%5D%5B0%5D=The+last+name+field+is+required.&%00%2A%00messages%5Bpassword%5D%5B0%5D=The+password+field+is+required.&%00%2A%00messages%5Bemail%5D%5B0%5D=The+email+field+is+required.&%00%2A%00format=%3Amessage

提前致谢

最佳答案

你将变量直接传递给路由,你应该这样做:

    if ($validator->fails())
{
$messages = $validator->messages();
return Redirect::action('MembersController@loadRegisterView')->withErrors($messages);
}else{
return "Thank you!";
}

关于php - laravel 通过 Controller 将错误传递给 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23014808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com