【发布时间】:2014-09-11 01:35:31
【问题描述】:
这周我决定从 CodeIgniter 迁移到 Laravel。 Atm 我正在尝试将我的旧 CI 网站“迁移”到 Laravel。
注意:我对 Laravel 很陌生,所以如果我做错了什么,请说 :) 我习惯了 CodeIgniter,我什至不知道这在 Laravel 中是否可以工作。
这就是我想要的 / 就像我在 codeigniter 中所拥有的那样 --> http://pastebin.com/DRaW0Mzt
public function add()
{
$data['header'] = "Create New User";
$data['title'] = "Create New User";
$data['description'] = "Create New User";
$data['user_groups'] = UserGroup::all();
return View::make('users.create', $data);
}
function create()
{
$validator = Validator::make(
array(
'email' => Input::get('email'),
'username' => Input::get('username'),
),
array(
'email' => 'required|email|unique:users',
'username' => 'required|min:3|max:14|unique:users',
)
);
if ($validator->fails())
{
/* If there's an error then just call the add function again */
/* $validator->messages(); should still be available since it is still the same "instance" */
/* This is how i did it on CodeIgniter */
$this->add();
}else{
/* success code... */
}
}
如果 $validator 失败,我希望它调用 $this->add();再次。 注意:我尝试过以下操作;
return Redirect::to('users/add')->withInput()->withErrors($validator); # redirect to users/add, WITH the old input and WITH the errors from $validator. */
问题 #1:调用 $this->add(); 时它调用函数,但不执行函数内部的代码,视图没有创建,所有的 $data 也没有传递给视图。
而且,如果我创建;
protected $layout = "layouts.master";
它将使用那个代替;
return View::make('users.create', $data);
但是没有 $data 将被传递给它。
问题 #2:使用 Redirect::to() 的问题;是如果用户重新加载页面然后所有 Input::old();自从 withInput() 和 withErrors() 之后就消失了;使用会话存储数据,它只持续一个请求,错误也消失了。 包括输入周围/上的 CSS https://dl.dropboxusercontent.com/u/11204765/SS/SS-026.png
注意:我尝试使用以下两个;
Session::reflash();
Session::keep(array(...));
有人知道问题的解决方案吗?还是我可能做错了什么?
感谢任何帮助!
【问题讨论】:
-
#1 使用
Redirect::to或Redirect::back()。 #2 为什么会有问题?如果 use 重新加载页面,那么它应该重新加载,不是吗? -
If i
Redirect::to()->withInput->()->withErrors();then 1) 如果我的视图文件中没有大量 if 语句,我将无法 dl.dropboxusercontent.com/u/11204765/SS/SS-026.png 在我的输入周围获取 CSS。 2)如果用户重新加载页面(无论出于何种原因)所有旧输入都消失了,错误也消失了,因为->withInput();和->withErros();利用会话,这意味着数据只持续1个请求,我希望它如果需要,最后几个请求,这就是为什么我也尝试使用Session::reflash();和Session::keep(); -
1 使用单个 css 类,不需要大量的 if 语句。 2 我明白,它应该是这样工作的——就像我说的,如果用户重新加载页面,那么应该重新加载页面。我就是这么看的。
-
1) 我仍然需要一个 if 语句来确定我是否需要/想要添加 CSS 类,还是我错了? 2)当然是的,但我也希望保留旧的输入和错误。
-
@deczo 类似这样,我需要 if 语句来确定我需要添加哪些 css 类,如果该字段被批准或包含错误 dl.dropboxusercontent.com/u/11204765/SS/SS-027.png(使用 Bootstrap CSS 仅供参考) .
标签: php codeigniter laravel