下面是我的控制器Admin/MoviesController的'store'方法示例.它看起来已经很大了,"更新"方法会更大.
算法是:
我的问题:
有一些最佳做法吗?我说的不是MVC,而是Laravel的功能.目前为了保持一些逻辑,我只使用Request for validation.
public function store(CreateMovieRequest $request) {
$movie = Movies::create($request->except('poster'));
/* Uploading poster */
if ($request->hasFile('poster')) {
$poster = \Image::make($request->file('poster'));
$poster->fit(250, 360, function ($constraint) {
$constraint->upsize();
});
$path = storage_path() . '/images/movies/'.$movie->id.'/';
if(! \File::exists($path)) {
\File::makeDirectory($path);
}
$filename = time() . '.' . $request->file('poster')->getClientOriginalExtension();
$poster->save($path . $filename);
$movie->poster = $filename;
}
/* If 'Meta Title' is empty, then fill it with the name of the movie */ …Run Code Online (Sandbox Code Playgroud)如果我具有以下表单字段(下面的代码),如何在模型中根据需要设置该字段,以便像其他字段一样,如果表单不包含信息,则无法提交该表单。
<?= $form->field($model, 'seo[seo_title]')->textInput(['maxlength' => 60])->label('SEO Title') ?>
Run Code Online (Sandbox Code Playgroud) 所以在Yii 1中,在视图文件中,您可以使用$this->action()或访问Controller的属性/操作$this->property.
在Yii 2中,我能看到这种可能性的唯一方法是使用Yii::$app->controller->property或Yii::$app->controller->action().我是一个不想写更多代码的人,所以我想知道是否有更短的方法.
我没有得到extract()函数的挂起,并且传输变量.我在用户控制器中有一个方法,其中定义了一些变量,并在数组中发送到父控制器中的视图函数,在该控制器中提取数组.然后视图是必需的.但变量未定义.可以打印数组内容.
以下是具有简化配置文件功能的用户控制器:
class User extends Controller{
public function profile(){
$profiledetails = $this->profiledetails();
$profilestatus = $this->profileStatus();
$this->view('profile', [$profiledetails, $profilestatus]);
}}
Run Code Online (Sandbox Code Playgroud)
变量被发送到父Controller中的view函数:
class Controller {
public function view($view, $variables =[]){
extract($variables);
require_once './app/views/' . $view . '.php';
}}
Run Code Online (Sandbox Code Playgroud)
在视图中,'profile.php',显示未定义的变量错误.我认为"extract()"函数会使$ profiledetails和$ profilestatus在视图中可用作变量.我究竟做错了什么?也许我正在使用错误类型的数组,或者我应该使用"变量变量"或其他东西..(在这种情况下,如何?).
基本上我的:创建动作测试保持失败,即使它在应用程序中工作.我在下面的控制器中注释了回形针验证并且它有效.
has_attached_file :image, styles: { medium: "700x700>", small: "350x250#" }
validates_attachment_presence :image
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
Run Code Online (Sandbox Code Playgroud)
这是我正在运行的测试,它在注释验证时有效.我怎样才能通过符合我模型中回形针验证的参数.
test "should create if signed_in" do
sign_in(@user)
assert_difference 'Product.count' do
post :create, product:{ title:'test_product', description: 'khara', user_id: @user.id}
end
assert_redirected_to product_path(assigns(:product))
end
Run Code Online (Sandbox Code Playgroud)
失败的消息:
FAIL["test_should_post_create_if_signed_in", ProductsControllerTest, 0.58458]
test_should_post_create_if_signed_in#ProductsControllerTest (0.58s)
"Product.count" didn't change by 1.
Expected: 3
Actual: 2
test/controllers/products_controller_test.rb:52:in `block in <class:ProductsControllerTest>'
Run Code Online (Sandbox Code Playgroud)
基本上我该怎么做这个测试通过?
注意:我知道回形针提供了测试指令,而Shoulda和Spec希望纯粹在Minitest中做到这一点.
所以我有一个具有以下结构的视图(这不是实际的代码,而是摘要):
@using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{
@Html.ValidationSummary("", new { @class = "text-danger" })
<table>
<thead>
<tr>
<th>Column1</th>
<th>Column2</th>
</tr>
</thead>
<tbody id="myTableBody">
@for (int i = 0; i < Model.Components.Count; i++)
{
@Html.EditorFor(m => m.MyCollection[i])
}
</tbody>
<tfoot>
<tr>
<td>
<button id="btnAddRow" type="button">MyButton</button>
</td>
</tr>
</tfoot>
</table>
<input type="button" id="btnSubmit" />
}
@section scripts {
@Scripts.Render("~/Scripts/MyJs.js")
}
Run Code Online (Sandbox Code Playgroud)
EditorFor是渲染标记,表示绑定到MyCollection中的属性的行.以下是编辑器模板外观的示例摘录:
@model MyProject.Models.MyCollectionClass
<tr>
<td>
@Html.TextBoxFor(m => m.Name)
</td>
<td>
@Html.DropDownListFor(m => m.Type, Model.AvailableTypes)
</td>
</tr>Run Code Online (Sandbox Code Playgroud)
基本上,我的问题是客户端验证不会触发编辑器模板内部的元素.有人能指出我可能出错的方向吗?
另请注意,我的web.config中设置了以下内容.
<appSettings>
<add key="ClientValidationEnabled" value="true" /> …Run Code Online (Sandbox Code Playgroud) javascript validation model-view-controller asp.net-mvc jquery
我非常清楚MVC模式,但Meteor本身似乎没有像它那样的任何模式.
在开发Meteor应用程序时使用哪种架构模式?
我想在我的网站前面放一些统计信息,例如:
# jobs/index.html.haml
.panel
.panel-heading
%h4 Statistics
.panel-body
.col-md-9.col-xs-9
%h5.pull-left Users
.col-md-3.col-xs-3
%h5.pull-right= @usercount
.col-md-9.col-xs-9
%h5.pull-left Companies
.col-md-3.col-xs-3
%h5.pull-right= @companycount
.col-md-9.col-xs-9
%h5.pull-left Categories
.col-md-3.col-xs-3
%h5.pull-right= @categorycount
.col-md-9.col-xs-9
%h5.pull-left Total offers gathered
.col-md-3.col-xs-3
%h5.pull-right= @jobcount
Run Code Online (Sandbox Code Playgroud)
目前这些变量都在控制器中设置:
# controller: jobs, action: index
@jobcount = Job.count
@usercount = User.count
@categorycount = Category.count
@companycount = Job.distinct.count('company')
Run Code Online (Sandbox Code Playgroud)
但我相信这是在MVC中做到这一点的最糟糕方式.谁能告诉我应该怎么做?
我有一个包含几个项目的Visual Studio解决方案:
MVC应用程序默认使用现有的类进行用户管理,它也有自己的ApplicationDbContext.在这种情况下,我们有两个DB上下文.
您对用户管理的体验是什么?如果我为用户管理创建自己的类并将它们放到Domain Classes项目中会更好吗?我认为后面的维护会更容易,在这种情况下只有一个DBContext.另一个可能的问题可能是现有的ApplicationUsers(来自MVC项目模型)和来自Domain Classes Project的类之间的关系.或者可能将ApplicationUser类定义移动到DomainClasses项目?
我导出了一个工作项目模板,并尝试使用它创建一个新项目.
这是我得到的错误.
Type IAppBuilder is not defined.
Type CookieAuthenticationOpetions is not defined
Type OwinStartupAttribute is not defined
Type IAppBuilder is not defined
DefaultAuthenticationTypes is not declared
Run Code Online (Sandbox Code Playgroud)
还有53个警告,包括
Could not resolve this reference...
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么这些不被延续?我真的不想为我从这个模板创建的每个项目都这样做.
更新
使用我创建的非mvc模板也会发生同样的事情.在导出之前我甚至没有做任何编码更改.
这里有一些
ScriptBundle is not defined
FriendlyURLSettings is not defined
BundleCollection is not defined
Run Code Online (Sandbox Code Playgroud)
更新2
Visual Studio 2012中也发生了同样的事情.(更新后的问题标题)
更新3
它在packages.config中显示这些引用.仍然不确定为什么他们没被引用:
<package id="EntityFramework" version="5.0.0" targetFramework="net45" />
<package id="jQuery" version="1.8.2" targetFramework="net45" />
<package id="jQuery.UI.Combined" version="1.8.24" targetFramework="net45" />
<package id="jQuery.Validation" version="1.10.0" targetFramework="net45" />
<package id="knockoutjs" version="2.2.0" targetFramework="net45" />
<package …Run Code Online (Sandbox Code Playgroud) .net model-view-controller project-template visual-studio nuget
php ×3
asp.net-mvc ×2
validation ×2
yii2 ×2
.net ×1
architecture ×1
arrays ×1
c# ×1
coding-style ×1
extract ×1
javascript ×1
jquery ×1
laravel ×1
laravel-5 ×1
meteor ×1
minitest ×1
nuget ×1
paperclip ×1
ruby ×1
variables ×1