我有一堆控制器在类级作用域中具有[AllowAnonymous]属性.在执行此类控制器中的任何操作之前,我需要检查一些事情.例如,我可能会这样做:
[AllowAnonymous]
public class MyController : Controller
{
[HttpGet]
public ActionResult Index()
{
// do some preliminary work
// run action-specific code
}
}
Run Code Online (Sandbox Code Playgroud)
因为我有很多这样的控制器和动作,所以复制粘贴相同的代码是一个繁琐的过程.有没有办法以更简单的方式为每个匿名操作执行此初步代码?
我有路由和控制器的问题.我有两种类型的控制器:第一种类型用于管理网页,第二种类型用于cms,我更喜欢将它们放在子文件夹中.例:
/controller/site.php (for webpages)
/controller/admin/ (for controllers to manage cms)
Run Code Online (Sandbox Code Playgroud)
在routes.php我写道:
$route['(:any)'] = "site/$1";
$route['admin/(:any)'] = "admin/$1";
Run Code Online (Sandbox Code Playgroud)
我有这样设置的文件.htacces:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|css|js|font|woff|ttf|svg|eot|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Run Code Online (Sandbox Code Playgroud)
和config.php上的这个变量:
$config['index'] = '';
Run Code Online (Sandbox Code Playgroud)
但它只适用于"网站".例如,如果我写"mywebsite/admin/login",则返回404错误.
我发现MY_Router也扩展了CI_Route但不起作用.
有人可以帮我解决这个问题吗?
我有FXML和keyPress处理程序的问题.
我尝试设置按键简单测试,但不调用键处理程序.鼠标处理等工作正常,但键处理没有.
我尝试在Pane上设置KeyHandler,圆圈(在Pane中)和场景.
public class Controller implements Initializable {
public Pane canvas;
public Label yDisplay;
public Label xDisplay;
public Circle circle;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
canvas.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
System.out.println("Test");
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
尊重Dil,thx为你的答案,但我想做一些"游戏",在窗格上的简单圆圈,通过按键在键盘上移动.当然,专注于compmonent会起作用.但没有专注.也许必须在窗口或场景上设置关键监听器?我为目的模糊而道歉.
我尝试按下按钮,但初始化中的场景为空.
public class Controller implements Initializable {
@FXML private Pane canvas;
@FXML private Label yDisplay;
@FXML private Label xDisplay;
@FXML private Circle circle;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
canvas.getScene().setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public …Run Code Online (Sandbox Code Playgroud) 在大多数示例中,我看到控制器是以这种方式编写的,数组作为第二个参数.我猜数组中列出了依赖项,然后将依赖项作为参数的匿名函数...
myApp.controller('DoubleCtrl', ['$scope', function($scope) {
$scope.double = function(value) { return value * 2; };
}]);
Run Code Online (Sandbox Code Playgroud)
然后我看到有时它们是以这种方式编写的,没有数组和独立列出的依赖项,只有匿名函数将它们作为参数.
myApp.controller('DoubleCtrl', function($scope) {
$scope.double = function(value) { return value * 2; };
});
Run Code Online (Sandbox Code Playgroud)
有什么不同?哪个更好,更喜欢......?
我正在尝试在我的应用程序中注册用户,同时保持模型中的所有业务逻辑,并尽可能少地在控制器中.为了实现这一点,我boot()在Class::creating()事件触发时在模型的方法中运行用户验证.如果验证失败,我只返回false,取消该事件.你可以在这里看到:
public static function boot() {
parent::boot();
User::creating(function(){
$validator = new Services\Validators\RUser;
if (! $validator->passes()) return false;
});
}
Run Code Online (Sandbox Code Playgroud)
您看到的验证器类只是规则,它包含一个getErrors()函数.
我的问题是,我怎样才能重写这个,以便我可以检索验证器的错误,以便以后在条件重定向中使用?
我的控制器postRegister()(在表单上单击提交时调用的函数)如下所示:
public function postRegister() {
$user = new User(Input::all());
$user->save();
}
Run Code Online (Sandbox Code Playgroud)
我知道我没有在控制器中正确处理它,所以我也很感激一些建议.
谢谢.
下面的代码表示在处理来自服务器的数据的每个控制器中重复相同代码模式的情况.经过长时间的研究和#angularjs的irc谈话,我仍然无法想象如何抽象代码,内联注释解释了这些情况:
myApp.controller("TodoCtrl", function($scope, Restangular,
CalendarService, $filter){
var all_todos = [];
$scope.todos = [];
Restangular.all("vtodo/").getList().then(function(data){
all_todos = data;
$scope.todos = $filter("calendaractive")(all_todos);
});
//I can see myself repeating this line in every
//controller dealing with data which somehow relates
//and is possibly filtered by CalendarService:
$scope.activeData = CalendarService.activeData;
//also this line, which triggers refiltering when
//CalendarService is repeating within other controllers
$scope.$watch("activeData", function(){
$scope.todos = $filter("calendaractive")(all_todos);
}, true);
});
//example. another controller, different data, same relation with calendar?
myApp.controller("DiaryCtrl", function($scope, Restangular,
CalendarService, …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有一个叫做index.blade路由的页面/index.在它的URL中,它有一些get像?order和的参数?type.
我想将这些$_get参数传递给我的路由控制器操作,从DB查询并将其结果数据传递给索引页面.我该怎么办?
我的问题很简单.今天,前端和后端都有很多框架,它们实现了MVC(模型 - 视图 - 控制)架构.
"MVC中的控制器"是Facade Design Pattern的一个例子吗?
model-view-controller design-patterns controller facade object-oriented-analysis
我在控制器中有一个XDocument,我想将该服务器作为xml和json服务器(取决于请求的Accept标头)。
我正在使用dotnet core:
在我的startup.cs / ConfigureServices中,我有以下内容:
services.AddMvc().AddXmlDataContractSerializerFormatters();
Run Code Online (Sandbox Code Playgroud)
我的控制器本质上是这样的:
public async Task<IActionResult> getData(int id)
{
XDocument xmlDoc = db.getData(id);
return Ok(xmlDoc);
}
Run Code Online (Sandbox Code Playgroud)
使用发出请求时Accept: application/json,我将数据正确格式化为JSON。使用发出请求时Accept: application/xml,我仍然会收到JSON响应(与相同application/json)。
我也尝试过:
services.AddMvc().AddXmlSerializerFormatters();
Run Code Online (Sandbox Code Playgroud)
但这甚至更糟,因为甚至将普通对象用作JSON(XmlDataContractSerializer可以处理普通对象,但不能处理XDocument)。
当我添加[Produces("application/xml")]到控制器(使用AddXmlSerializerFormatters)时,在提供XDocument时出现Http 406错误,但是在返回普通对象时确实得到了XML输出。
我是否必须将XDocument转换为对象以从控制器输出XML?有没有简单的方法可以将XDocuments转换为对象?
遇到ember(v.3.x)的问题,无法找出原因.我没有在文档中找到任何内容(在我的情况下有效).
所以,这就是问题所在:这会在我的控制器中返回undefined在computed属性中(undefined.get('filter')).我的其余功能都有效,所以我没有把它放在这里.
// in dic.js (controller):
export default Controller.extend({
filter: '',
filteredTerms: computed('filter', () => {
const x = this.get('filter');
// ...
}),
Run Code Online (Sandbox Code Playgroud)
相应的输入字段:
// in dic.hbs
{{input value=filter}}
Run Code Online (Sandbox Code Playgroud)
在我的路线中,我只返回模型,该模型应该由该函数过滤.
controller ×10
angularjs ×2
laravel ×2
routes ×2
.net ×1
action ×1
asp.net-core ×1
asp.net-mvc ×1
codeigniter ×1
dry ×1
ember.js ×1
facade ×1
fxml ×1
handle ×1
java ×1
javafx ×1
javascript ×1
laravel-5.2 ×1
model ×1
php ×1
restangular ×1
this ×1
undefined ×1
url ×1
validation ×1
xml ×1