[英]Laravel 4 database actions - controller or model
just started using Laravel but want to make sure I am using it correctly. 刚开始使用Laravel,但要确保我正确使用了它。
Most of my work is CMS based so read / write / update etc to a database. 我的大部分工作是基于CMS的,因此对数据库进行读/写/更新等操作。
An example of what I have done so far is an insertion into the DB: 到目前为止,我所做的一个例子是插入数据库:
On the view I have a form with a URL of 'addNewUser'. 在视图上,我有一个URL为'addNewUser'的表单。
In my routes I then do: 然后在我的路线中:
Route::post('addnewuser', array('uses' => 'UserController@addNewUser'));
My user controller 'addNewUser' method is (simplified): 我的用户控制器“ addNewUser”方法为(简化):
public function addNewUser() {
$data = Input::all();
$rules = array(
'username' => 'required|alpha_dash|max:16|unique:users,username',
);
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails())
{
Input::flash();
$errors = $validator->messages();
return Redirect::to('/register')->withErrors($validator)->withInput();
}
$user = new User;
$user->save();
return Redirect::to('/login')->with('successLogin', '1');
}
Is this correct? 这个对吗? I have read somewhere that all DB interaction should be in the model?
我读过某个地方所有数据库交互都应该在模型中?
Likewise when reading from the DB to display a foreach for example, I do the following directly in the view: 同样,从数据库读取以显示foreach时,例如,我直接在视图中执行以下操作:
$builds = DB::table('blogs')->orderBy('id', 'desc')->get();
if ($builds) {
foreach ($builds as $build)
{
$safeURLSlug = stringHelpers::safeURLSlug($build->blogtitle);
echo "
// stuff
";
}
} else {
// no stuff
}
Should I be doing these sort of queries and showing of data directly in the view? 我应该进行此类查询并直接在视图中显示数据吗? or in a model / controller function etc?
或在模型/控制器功能等中?
Want to check im doing things 100% correct / the standard way of doing things before I get too involved. 我想参与进来之前,要检查即时消息处理服务是否100%正确/标准的处理方法。
I can see a few things that I personally would have done differently. 我可以看到一些我个人会做的不同的事情。
For example I usually put $rules
as a class variable so it can be used in different functions related to your Users. 例如,我通常将
$rules
用作类变量,以便可以在与您的Users相关的不同功能中使用它。
Have you tested your code yet? 您已经测试过代码了吗? Any errors?
有什么错误吗?
In your addNewUser
function does it save any data? 在您的
addNewUser
函数中,它是否保存任何数据? I know you have "simplified" above the code snippet but there should be $user->username = $data['username'];
我知道您已经在代码段上方“简化了”,但是应该有
$user->username = $data['username'];
etc. in between creating your $user
variable and running $user->save();
在创建
$user
变量和运行$user->save();
等等$user->save();
, so if you excluded this on purpose then I don't see anything else with your model. ,因此,如果您有意排除此情况,那么您的模型将看不到其他任何内容。
In your view code, $builds = DB::table('blogs')->orderBy('id', 'desc')->get();
在您的视图代码中,
$builds = DB::table('blogs')->orderBy('id', 'desc')->get();
should be done in your controller and passed to your view like so return View::make('example', array('builds' => $builds))
应该在您的控制器中完成并传递给您的视图,例如
return View::make('example', array('builds' => $builds))
I'd also change 我也会改变
$builds = DB::table('blogs')->orderBy('id', 'desc')->get();
to 至
$builds = Blog::orderby('id','desc')->get();
if you have a Blog
model, otherwise your code is fine. 如果您具有
Blog
模型,则您的代码很好。
You could move: 您可以移动:
$rules = array(
'username' => 'required|alpha_dash|max:16|unique:users,username',
);
to User model as static variable, and instead of: 用户模型作为静态变量,而不是:
$validator = Validator::make($data, $rules, $messages);
you could use: 您可以使用:
$validator = Validator::make($data, User::$rules, $messages);
But definitely you shouldn't get data from database in your View, this code should be in controller, for example: 但是绝对不应该在View中从数据库中获取数据,此代码应该在控制器中,例如:
$builds = DB::table('blogs')->orderBy('id', 'desc')->get();
return View::make('someview')->with('builds', $builds);
of course if you have Blog
model, you should use here: 当然,如果您具有
Blog
模型,则应在此处使用:
$builds = Blog::orderBy('id', 'desc')->get();
return View::make('someview')->with('builds', $builds);
It's also unclear what the following code does: 还不清楚以下代码的作用:
$safeURLSlug = stringHelpers::safeURLSlug($build->blogtitle);
but probably you could move it to your Blog
model and use accessor to make the change: 但可能您可以将其移至
Blog
模型并使用accessor进行更改:
public function getSafeSlugAttribute($value) {
return stringHelpers::safeURLSlug($this->blogtitle);
}
and now your view could look like this: 现在您的视图应如下所示:
@foreach ($builds as $build)
{{{ $build->title }}} {{{ $build->safeSlug }}}
@endforeach
I suggest you take a look on Laravel Generators. 我建议您看一下Laravel Generators。
https://github.com/JeffreyWay/Laravel-4-Generators https://github.com/JeffreyWay/Laravel-4-Generators
Install and then run: 安装并运行:
php artisan generate:scaffold customer
Laravel line command generator create a basic CRUD for you with controller, model, views and database migrations. Laravel行命令生成器通过控制器,模型,视图和数据库迁移为您创建基本的CRUD。 That's good to safe time and keep your project with some default organization.
这样可以确保安全的时间,并让您的项目使用默认的组织机构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.