如果我具有以下表单字段(下面的代码),如何在模型中根据需要设置该字段,以便像其他字段一样,如果表单不包含信息,则无法提交该表单。
<?= $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在视图中可用作变量.我究竟做错了什么?也许我正在使用错误类型的数组,或者我应该使用"变量变量"或其他东西..(在这种情况下,如何?).
我试图通过在mvc应用程序中使用SignalR向所有客户端广播消息。我遇到的问题是当我使用此代码时
var context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
Run Code Online (Sandbox Code Playgroud)
上下文没有客户端,因此不会广播该消息。下面是我正在使用的代码的简化版本。我想念什么吗?谢谢
风景:
@using System.Web.UI.WebControls
@using MyApp.Models
@model MyApp.Models.MyModel
<form class="float_left" method="post" id="form" name="form">
<fieldset>
Username: <br/>
@Html.TextBoxFor(m => m.Username, new { Value = Model.Username })
<br/><br/>
<input id="btnButton" type="button" value="Subscribe"/>
<br/><br/>
<div id="notificationContainer"></div>
</fieldset>
</form>
@section scripts {
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="~/signalr/hubs"></script>
<script>
$(function () {
var notification = $.connection.notificationHub;
notification.client.addNewMessageToPage = function (message) {
$('#notificationContainer').append('<strong>' + message + '</strong>');
};
$.connection.hub.start();
});
$("#btnButton").click(function () {
$.ajax({
url: "/Home/Subscribe",
data: $('#form').serialize(),
type: "POST"
});
}); …Run Code Online (Sandbox Code Playgroud) 我有一个搜索框,可以使用EMPLID进行搜索.搜索有效,但如果我在搜索后转到任何其他页面(例如:如果我切换到详细信息页面)然后导航回我搜索的页面,它会显示所有记录.如何保持搜索条件,以便在我在页面之间导航时显示该EMPLID的信息?
我的控制器:
public ActionResult Index(string SearchString)
{
var emp = from e in db.EMPLOYMENTs
select e;
if (!String.IsNullOrEmpty(SearchString))
{
emp = emp.Where(s => s.EMPLID.Contains(SearchString));
}
return View(emp);
}
Run Code Online (Sandbox Code Playgroud)
我的布局:
@using (Html.BeginForm())
{
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input class="form-control" placeholder="Search" type="text" name="SearchString">
</div>
</form>
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个多语言网站,其所有者需要大多数配置/选项 - 必须先设置 - 才能存储在数据库中,例如:
我很困惑在哪里查询这个配置(因为我仍然在MVC方法背后的逻辑).
应用程序根目录包含以下内容:
在Symfony/Doctrine/PHP项目中,客户抱怨我们破坏了软件开发最佳实践.投诉是关于源代码的不正确分层以及缺乏单元测试.
正如我们所看到的,我们一直在使用适当的MVC.
($this->get('security.context')->isGranted('ROLE_ADMIN')和$this->get('security.context')->getToken()->getUser().请注意,Symfony已经改变了模型,因为我们开始了这个项目 - 但仍保持向后兼容.
在控制器中,客户明确表示控制器处理错误:
return $this->render('some_template.html.twig');)客户端说最佳实践是让控制器简单地将请求传递到系统中的另一层.
此外,他说用户管理员基于"自定义模型",其中所有用户和角色都存储在数据库中 - 这使得插入不同的访问控制系统变得困难.特别是因为角色名称似乎是硬编码的,例如通过命令($this->get('security.context')->isGranted('ROLE_ADMIN').
所以; 这个领域有最终的最佳实践吗?什么属于控制器,并且Doctrine,Twig,Symfony Security"足够"是"控制器下方"的单独层.
例如,控制器和Doctrine之间是否还有另一层?
实际上,我正在与Laravel5(laravel.com)合作开展一个项目
正在使用SailsJS(sailsjs.org)处理另一个项目的人问我为什么还在使用PHP.我应该使用nodejs(sails),因为PHP将是一种语言将会死亡.
那么,未来有什么好用的.
什么更好 - 一个重要的方面 - 哪个框架创建响应更快?
将规范链接设置为井符号是否合法,如下所示,还是我需要输入物理页面名称?
<link rel="canonical" href="#">
Run Code Online (Sandbox Code Playgroud)
测试时,磅设置不会产生验证错误(ala #development = 1).在我的方案中,使用此布局文件的页面将没有备用的"常规HTML"版本.唯一的版本是AMP HTML版本.
对于其他上下文,我正在尝试使用AMP HTML的MVC网站.为了保持我的布局文件简单,我更喜欢使用井号而不是提取子页面名称并将其应用于href属性.我知道如何通过以下代码将URL应用于局部视图:
<link rel="canonical" href="@HttpContext.Current.Request.Url.AbsoluteUri">
Run Code Online (Sandbox Code Playgroud)
我只是好奇它是否是使用英镑符号的合法AMP HTML.谢谢.
张贴在下面
我们无法弄清楚为什么UrlHelper在从WebApi控制器的上下文中使用时返回空字符串.
我们已经完成了必要的调试,但我们无法找到为什么会发生这种情况,RouteData中有路由,但它似乎没有工作.
在大多数情况下,我们使用RenderViewToString函数,该函数加载由调用组成的视图Url.RouteUrl(routeName).
已经尝试过的东西是创建自定义UrlHelper(但无济于事)并使用UrlHelper(MVC/HTTP)进行调试.
属性路由随处使用路由名称.
用法代码示例:
public class WebApiController : BaseApiController
{
[HttpPost]
[ResponseType(typeof(string))]
[Route("cart/get/checkout", Name = "api.cart.get.checkout")]
public IHttpActionResult GetCheckOutShoppingCart([FromBody] string data)
{
return Ok(RenderViewToString("CartController", "_CheckOutCartPartial", new ShoppingCartModel(Auth.IsAuthenticated ? Auth.GetCustomer().DefaultShippingInfo.CountryId : 148)
{
AddInsurance = false,
InsuredShipping = insuredShipping,
CurrentDeliveryMethodId = deliveryMethodId,
CurrentPaymentMethodId = paymentMethodId
}));
}
}
Run Code Online (Sandbox Code Playgroud)
BaseApiController类:
public class BaseApiController : ApiController
{
public static string RenderViewToString(string controllerName, string viewName)
{
return RenderViewToString(controllerName, viewName, new Dictionary<string, object>());
}
public static string RenderViewToString(string …Run Code Online (Sandbox Code Playgroud)