【发布时间】:2017-02-27 15:40:42
【问题描述】:
Controller 和 Routes 之间的区别基本上是什么。我们可以使用路由文件来控制我们的数据,那我们为什么需要控制器呢?
喜欢:
<?php
// app/routes.php
// route to process the ducks form
Route::post('ducks', function()
{
// process the form here
// create the validation rules ------------------------
$rules = array(
'name' => 'required', // just a normal required validation
'email' => 'required|email|unique:ducks', // required and must be unique in the ducks table
'password' => 'required',
'password_confirm' => 'required|same:password' // required and has to match the password field
);
// do the validation ----------------------------------
// validate against the inputs from our form
$validator = Validator::make(Input::all(), $rules);
// check if the validator failed -----------------------
if ($validator->fails()) {
// get the error messages from the validator
$messages = $validator->messages();
// redirect our user back to the form with the errors from the validator
return Redirect::to('ducks')
->withErrors($validator);
} else {
// validation successful ---------------------------
// our duck has passed all tests!
// let him enter the database
// create the data for our duck
$duck = new Duck;
$duck->name = Input::get('name');
$duck->email = Input::get('email');
$duck->password = Hash::make(Input::get('password'));
// save our duck
$duck->save();
// redirect ----------------------------------------
// redirect our user back to the form so they can do it all over again
return Redirect::to('ducks');
}
});
好吧,这不是我的代码,我在某个地方读到过,但是,这里这个人使用了 routes.php 文件中的验证,而在我的项目中,我在名为 UserController 的控制器中使用了验证技术,有什么区别它使?
【问题讨论】:
-
享受将 100 个函数用于 100 种不同的事物放在一个文件中。控制器也是完全不同的东西,查一下。
-
另外,如果你只有一个基于闭包的路由,你就不能使用路由缓存。
标签: laravel