标签: controller

在default.ctp中显示当前登录用户的最佳方法?

我正在自定义default.ctp文件,我正在尝试在页面顶部显示当前登录的用户名.

在app_controller.php,我有以下内容:

function beforeFilter()
{
    $user = $this->Auth->user();

    if($user != null)
    {
        $this->Session->write('user_name',$user['User']['username']);
    }
}
Run Code Online (Sandbox Code Playgroud)

在default.ctp,我有:

$user = $this->Session->read('Auth.User');

if(!empty($user))
{
    echo 'Hello, ' . $user['user_name'];
}
Run Code Online (Sandbox Code Playgroud)

但是,似乎价值$user_name没有在任何地方设置.

我究竟做错了什么?有没有更好的方法来实现这一目标?


更新:我已按照答案中的说明对其进行了修改,但仍然无效.我收到一个错误:

未定义的索引:user_name [APP/views/layouts/default.ctp,第21行]

authentication controller cakephp

8
推荐指数
1
解决办法
6033
查看次数

Spring 3 MVC使用单个Controller处理多个表单提交

Spring 3 MVC处理多个表单提交控制器.

我正在开发具有多种形式的JSP页面.1)搜索客户,2)搜索产品,3)打印东西等我有一个不同的表单绑定对象绑定到每个表单,我的控制器代码看起来类似于下面

  @Controller
  @RequestMapping(value="/search.do")
  public class SearchController {

    @RequestMapping(method = RequestMethod.GET)
    public String pageLoad(ModelMap modelMap) {
      modelMap.addAttribute("productSearch", new ProductSearchCriteria());
        modelMap.addAttribute("customerSearch", new CustomerSearchCriteria());
        modelMap.addAttribute("print", new PrintForm());
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView searchProducts(@ModelAttribute("productSearch") ProductSearchCriteria productSearchCriteria,
            BindingResult result, SessionStatus status) {
            //Do Product search
            return modelAndView;
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView searchCustomers(@ModelAttribute("customerSearch") CustomerSearchCriteria customerSearchCriteria,
            BindingResult result, SessionStatus status) {
            //Do Customer search
            return modelAndView;
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView printSomething(@ModelAttribute("print") PrintForm printForm,
            BindingResult result, SessionStatus status) {
            //Print …
Run Code Online (Sandbox Code Playgroud)

controller spring-mvc java-ee

8
推荐指数
3
解决办法
5万
查看次数

在ExtJS 4 MVC中使用多个控制器

假设我有一个主控制器,那么我的应用程序为每个"模块"都有一个控制器.这个主控制器包含视口,然后是一个标题(带有一个菜单)+一个"居中"的容器,它在开头是空的.

菜单中的单击将更改当前模块/控制器,并且adhoc视图(属于此控制器)将显示在居中容器中.

我认为这是一个非常简单的场景,但奇怪的是我没有找到合适的方法来做到这一点.有任何想法吗?非常感谢!

controller extjs extjs4 extjs-mvc

8
推荐指数
1
解决办法
2万
查看次数

模型中Rails应用程序的基本URL

为了能够看到用户是否在Facebook上共享了我的页面,我希望能够创建这种URL:

http://graph.facebook.com/?id=http://stylehatch.co/some_unique_user_token
Run Code Online (Sandbox Code Playgroud)

凡http://stylehatch.co将需要我的基本URL.我正在考虑在User模型中创建一个将构建并返回该URL的方法,但我无法从我的模型中访问root_url.

如何从模型中获取应用程序的基本URL?

如果我的网址看起来像这样:

http://myapp.com/users/new

http://myapp.com/users/2/show
Run Code Online (Sandbox Code Playgroud)

如何从我的模型中获取" http://myapp.com "?我已经添加了:

  include Rails.application.routes.url_helpers
Run Code Online (Sandbox Code Playgroud)

在我的模型中,但似乎root_url是零.有什么想法吗?这是一个Model方法是正确的,还是应该将它放在帮助器中?

谢谢

ruby controller ruby-on-rails request

8
推荐指数
2
解决办法
2万
查看次数

将Textarea发布到Controller(当前为null) - MVC

我正在创建一个网站,用户将填写文本区域(回答问题),然后按下一步.当他们按下时,textarea将被提交给控制器,下一个问题将被检索(我也将答案存储在cookie中).不幸的是,当我在调试中运行代码时,我意识到textarea没有被提交b/c参数为null.我试图解决它,我环顾四周,似乎正在做得很好.希望这是一个简单的解决方案.非常感谢你的时间!

控制器:

// POST: /Question/1 
[HttpPost]
public ActionResult q(string textAnswer) {
    if (textAnswer != null)
        ViewBag.current++;

    Question q = db.Questions.Find(ViewBag.current);
    if (q == null) {
        return RedirectToAction("submit");
    }

    return View(q);
}
Run Code Online (Sandbox Code Playgroud)

视图:

<form class="form-horizontal" id="myForm" method="post" enctype="text/plain" name="input">

    <p>
        <h3>Question <span id="integer">@ViewBag.current</span></h3>
        @Html.DisplayFor(model => model.Ques)
    </p>

    <div class="control-group">
        <label class="control-label">Answer</label>
        <div class="controls">
            <textarea rows="20" style="width:600px" id="textAnswer"></textarea>
        </div>
    </div>

    <div class="control-group">
        <div class="controls" >
            <button onclick="history.back()" type="button" class="btn">Back</button>
            <input type="submit" class="btn" value="Next" />
        </div>
    </div>

</form>
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc post controller

8
推荐指数
1
解决办法
1万
查看次数

路径'/'的控制器未找到或未在Sitecore中实现IController

我正在从这里学习Sitecore中的Controller渲染.

我创建了一个简单的控制器(HelloWorld)和相关视图(Index.schtml).在Sitecore Explorer的渲染部分中映射它(使用Name PageContent)并在Sitecore Explorer的内容部分中的Home Item中添加渲染项目.但是当我浏览它时,它会给出错误.

The controller for path '/' was not found or does not implement IController. 
Run Code Online (Sandbox Code Playgroud)

我读过的所有帖子都与Asp .Net MVC有关..但我有与Sitecore MVC有关的问题

Sample.html(Sitecore Explorer渲染部分中的页面内容)

@using Sitecore.Mvc

<html>
<body>
    @Html.Sitecore().Placeholder("content")

    <p>Today's date is @DateTime.Now.ToShortDateString()</p>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

只有这条线给出了问题

@Html.Sitecore().Placeholder("content")
Run Code Online (Sandbox Code Playgroud)

如果我删除此行...它工作正常,浏览器上的页面显示日期和时间

的index.html

<p>Hello from Controller -todays Date is @DateTime.Now.ToString()</p>
Run Code Online (Sandbox Code Playgroud)

调节器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC.Controllers
{
    public class HelloWorldController : Controller
    {
        //
        // GET: /HellowWorld/

        public ActionResult Index()
        {
            return View();
        } …
Run Code Online (Sandbox Code Playgroud)

.net c# controller sitecore sitecore-mvc

8
推荐指数
2
解决办法
3万
查看次数

在AngularJS中的另一个控制器中使用控制器

为什么我无法绑定到第二个控制器内的控制器变量?

<div ng-app="ManagerApp">
    <div ng-controller="MainCtrl">
        Main: 
        <div ng-repeat="state in states">
            {{state}}
        </div>
        <div ng-controller="InsideCtrl as inside">
            Inside:
            <div ng-repeat="state in inside.states2">
                {{state}}
            </div>
        </div>
    </div>
</div>

var angularApp = angular.module('ManagerApp', []);

angularApp.controller('MainCtrl', ['$scope', function ($scope) {
    $scope.states = ["NY", "CA", "WA"];
}]);

angularApp.controller('InsideCtrl', ['$scope', function ($scope) {
    $scope.states2 = ["NY", "CA", "WA"];
}]);
Run Code Online (Sandbox Code Playgroud)

示例:https://jsfiddle.net/nukRe/135/

第二次ng-repeat不起作用.

binding controller angularjs angularjs-controller angularjs-controlleras

8
推荐指数
1
解决办法
1万
查看次数

是否可以在 Asp Net Web Api 控制器中使用事件处理程序?

我有一个 web api 控制器,我想在其中使用事件处理程序。更具体地说,我有一个MessagesController,当执行 action 方法时,WriteMessage我想触发一个SignalR Hub method将订阅a 的事件,该方法将向所有必要的接收者广播消息。看看下面的简化代码:

这是控制器:

public class MessagesController : ApiController
{
    private readonly IMessageService _messageService;
    public EventHandler<MessageEventArgs> MessageSent;

    public MessagesController(IMessageService messageService)
    {
        _messageService = messageService;
    }

    public IHttpActionResult WriteMessage(Message message)
    {
        try
        {
            //here I'm using MessageService to write the message to the data base
            _messageService.WriteMessage(message);

            //after the message was saved successfully I would like to trigger the event that will fire a signalR hub's method
            OnMessageSent(message);
            return Content(HttpStatusCode.Created, …
Run Code Online (Sandbox Code Playgroud)

asp.net controller event-handling signalr signalr-hub

8
推荐指数
0
解决办法
1269
查看次数

调用 api 作为应用程序启动的一部分?

我目前面临的情况是,我需要在应用程序启动过程中调用控制器?控制器由应用程序本身托管。这有可能吗?只需要在每次应用程序启动时触发即可。

c# rest controller asp.net-core

8
推荐指数
1
解决办法
6744
查看次数

控制器没有容器集,是不是忘记定义为服务订阅者了

我正在尝试重构 Symfony 5 服务器中的一些控制器,但由于以下错误,我突然无法更改或创建控制器:

'App\Controller{{ControllerName}}' 没有设置容器,您是否忘记将其定义为服务订阅者?

这是控制器:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Shiftsummary;
use App\Entity\Station;
use App\Entity\Shift;
use App\Entity\Line;
use \Datetime;

class StartStationController extends AbstractController {
    /**
     * @Route("/start", name="StartStation")
     */

    public function route(Request $request)
    {
      ...
    }  }
Run Code Online (Sandbox Code Playgroud)

这是service.yaml的内容

services:
    _defaults:
        autowire: true  
        autoconfigure: true 

    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']
Run Code Online (Sandbox Code Playgroud)

控制器位于src/controller/文件夹中

在此处输入图片说明

php controller symfony

8
推荐指数
3
解决办法
7978
查看次数