Laravel - 刀片中的模型常量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30778727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - Model const in a blade?
提问by I'll-Be-Back
Is it bad practice to use Model:CONST in a blade view or what is other approach?
在刀片视图中使用 Model:CONST 是不好的做法还是其他方法?
For example in the model, I have like this:
例如在模型中,我有这样的:
class ServiceType extends Eloquent
{
protected $table = 'service_type';
const TYPE_LANDLINE = 1;
const TYPE_SIP = 4;
}
and in the controller:
并在控制器中:
if ($packageDb->service_type_id == ServiceType::TYPE_SIP) {
$summary[service_type] = $packageDb->service_type_id;
}
if ($packageDb->service_type_id == ServiceType::TYPE_LANDLINE) {
$summary[service_type] = $packageDb->service_type_id;
}
return View::make("order.order-billing")->with('summary', $summary);
In blade I could do something like this (not tested):
在刀片中,我可以做这样的事情(未测试):
@if ($summary['service_type'] == ServiceType::TYPE_SIP)
..
@endif
回答by totymedli
tl;dr
tl;博士
It is up to you.
它是由你决定。
Alternate solution
替代解决方案
You could create a variable for your constant and pass it to the view:
您可以为常量创建一个变量并将其传递给视图:
$roleNames = User::ROLE_NAMES;
return View::make("membership.edit", compact('roleNames'));
Then in the view:
然后在视图中:
<td>@lang("app.{$roleNames[$member->pivot->role_id]}")</td>
Advantages
好处
- Shorter:You don't have to write the fully qualified name of the model. This is pretty handy if you have a deep model structure or long model names.
- You can "rename" the constant:Sometimes constant names are general. By passing a variable you can give a more descriptive name for the variable that can tell how exactly you use those constants in that given context.
- It is clearer what the view works with:The controller's job is to provide the needed resources (for the view) to generate a response to the request. If you pass the constants to the view in the controller, you can see what resources the view works with.
- 更短:您不必编写模型的完全限定名称。如果您有很深的模型结构或很长的模型名称,这将非常方便。
- 您可以“重命名”常量:有时常量名称是通用的。通过传递一个变量,您可以为变量提供一个更具描述性的名称,该名称可以告诉您在给定的上下文中如何准确地使用这些常量。
- 视图的作用更加清晰:控制器的工作是提供所需的资源(用于视图)以生成对请求的响应。如果将常量传递给控制器中的视图,则可以看到该视图使用哪些资源。
Disadvantages
缺点
Of course there can be down sides too, when using this method is cumbersome. If you have many constants (for example for each user role) then probably you don't want to pass all of them to the view, because you will end up with something like this:
当然也有不利的一面,使用这种方法很麻烦。如果您有许多常量(例如对于每个用户角色),那么您可能不想将所有常量都传递给视图,因为您最终会得到如下内容:
$noRole = User::NO_ROLE;
$memberRole = User::MEMBER_ROLE;
$adminRole = User::ADMIN_ROLE;
$moderatorRole = User::MODERATOR_ROLE;
$reviewerRole = User::REVIEWER_ROLE;
$publisherRole = User::PUBLISHER_ROLE;
return View::make("membership.edit", compact(
'noRole',
'memberRole',
'adminRole',
'moderatorRole',
'reviewerRole',
'publisherRole'
));
The main problems with this:
这方面的主要问题:
- Lot of unnecessary codefor a trivial functionality.
- Hard to maintain, especially if your view uses only a few of them.
- Violates DRY, especially if you need to do this in almost all function that returns a view.
- 大量不必要的代码用于微不足道的功能。
- 难以维护,尤其是当您的视图仅使用其中的几个时。
- 违反DRY,特别是如果您需要在几乎所有返回视图的函数中执行此操作。
Of course you could refactor this, create helper functions, but why would you deal with all of this hassle when (in this case) using the constants directly in the view is straightforward and easy to understand:
当然你可以重构它,创建辅助函数,但是当(在这种情况下)直接在视图中使用常量很简单易懂时,你为什么要处理所有这些麻烦:
@if ($user->role === App\User::ADMIN_ROLE)
The rule of thumb is to use the solution that is easier to read and understand. Except if you have a style guide, then you should follow that.
经验法则是使用更易于阅读和理解的解决方案。除非你有风格指南,否则你应该遵循它。
回答by Rok Jarc
In your blade file you can inject the model
在您的刀片文件中,您可以注入模型
@inject('ServiceTypeModel', 'App\Models\ServiceType')
and then use constants like this
然后使用这样的常量
{{ ServiceTypeModel::SIP }}
or
或者
@if ($x < ServiceTypeModel::SIP)...