标签: controller

Laravel控制器布局测试

我正在尝试将单元测试包含在我正在构建的新Laravel应用程序中.现在我想测试我的OrderController.该控制器的索引方法如下所示:

public function index()
{
    // We need the extra 'orders()' for the query scope
    $orders = $this->order->orders()->paginate($this->perPage);

    $this->layout->content = View::make('orders.index', compact('orders'));
}
Run Code Online (Sandbox Code Playgroud)

现在我的测试看起来像这样:

public function testIndex()
{
    $this->call('GET', 'orders');

    $this->assertResponseOk();

    $this->assertViewHas('orders');
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我运行phpunit,测试确实运行,但我得到: 1) OrderControllerTest::testIndex Failed asserting that an array has the key 'orders'.

我已经跟踪了使用Controller Layouts的问题$this->layout.如果我只是做return View::make()测试确实通过,如果我返回$this->layout...它也确实通过,但这会破坏实际的应用程序.

因此,我发现只有选项才能使用return View::make()并拥有@extends('master')该视图.但是我觉得如果你想测试它就不能在你的应用程序中使用Controller Layouts,这似乎很奇怪.

难道我做错了什么?

php unit-testing controller laravel laravel-4

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

使用服务AngularJS在两个控制器之间共享动态数据

我有两个角度服务,需要共享模型(消息列表和单个消息),他们从调用我们的API获得.服务如下:

angular.module('CmServices', ['ngResource'])
.factory('Messages', function ($resource, $routeParams, $rootScope) { 

    var data = {};

    data.rest = $resource(url, {}, {
            query: {method:'GET', params: params},
            post: {method:'POST', params: params}
        });

    // Trying to set this through a call to the API (needs to get param from route)
    $rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
            var messages = data.rest.query({m_gid: $routeParams.gid}, function () { 
                data.messages = messages;
            });
    });

    return data;    
});
Run Code Online (Sandbox Code Playgroud)

和控制器是:

function MessagesCtrl ($scope, $http, $location, $routeParams, Messages) {
   $scope.messages = Messages.messages;
}

function MessageCtrl …
Run Code Online (Sandbox Code Playgroud)

javascript rest controller angularjs

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

Flash和redirect_to在一行中不产生闪存

我有以下代码

redirect_to questionnaire_url, error: "Now please fill in the questionnaire."
Run Code Online (Sandbox Code Playgroud)

通常,如果我flash[:notice]在重定向之上设置一个值,闪存会显示在我的页面上,但是当我使用上面的代码时,它不会.

我显示闪光灯的代码是

- flash.each do |key, value|
  = content_tag(:div, value, class: "alert alert-#{key}")
Run Code Online (Sandbox Code Playgroud)

写完之后= flash.inspect我收到了

#<ActionDispatch::Flash::FlashHash:0x000000044322f0 @used=#<Set: {}>, @closed=false, @flashes={}, @now=nil>
Run Code Online (Sandbox Code Playgroud)

这当然是使用SLIM模板语言编写的.我的问题是notice实际生产的内容以及如何调整代码来修复它?谢谢.

controller ruby-on-rails view ruby-on-rails-3 slim-lang

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

Spring MVC - 返回静态页面

我正在努力尝试从弹簧MVC控制器返回静态网页.我遵循了这个教程:http://www.tutorialspoint.com/spring/spring_static_pages_example.htm但它仍然无法正常工作.

这是我定义配置(使用的配置类)的方式:

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan({ "com.my.web.api"})
@ImportResource("classpath:db-context.xml")
public class ApiServletConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/resources/");
        internalResourceViewResolver.setSuffix("*.html");
        return internalResourceViewResolver;
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器方法:

@RequestMapping(value = "/{id}/view", method = RequestMethod.GET, produces = "text/html")
@ResponseBody
public String getPostByIdHtml( @PathVariable String id) throws IOException {
    return "/resources/Post.html";
}
Run Code Online (Sandbox Code Playgroud)

在webapp文件夹下有一个名为"resources"的文件夹,下面有一个文件"Post.html".我还应该做些什么才能将此页面作为HTML返回而不是获取字符串"resources/Post.html"?

谢谢您的帮助.

html java servlets controller spring-mvc

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

grails controller:访问具有元素列表的参数

我需要访问存储在参数中的项目,该参数表示多选中的所选元素.我使用以下代码将选定的项目从gsp传递给控制器​​到remoteFunction:

params: '\'receiptItemsSelected=\' + jQuery(this).val()'
Run Code Online (Sandbox Code Playgroud)

现在,按照此处讨论中的代码,我使用闭包来获取每个值,但是如果我执行多选,则receiptItemsSelected的大小始终为1,但值例如为1,2.要将值作为列表获取,我在控制器中完成了以下操作

params.list("receiptItemsSelected")
Run Code Online (Sandbox Code Playgroud)

但如果我在multiselect中选择两个项目,但它总是只有一个元素,它就不会给我两个元素.问题是:如果我选择两个元素,我如何获得每个元素并在控制器中使用它?我怎么能把那些元素当作长而不是字符串呢?谢谢

grails controller list

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

使用Rails创建RSS源

我正在尝试为我的新闻帖子创建一个RSS提要,我用谷歌搜索它并提出这个代码:

def feed
  @posts = News.all(:conditions => "#{Settings.show} = 1", :select => "id, title, heading, content, date_posted", :order => "date_posted DESC") 

  respond_to do |format|
    format.rss { render :layout => false }
  end
end
Run Code Online (Sandbox Code Playgroud)

然后在一个名为"feed.rss.builder"的文件中我有这个:

xml.instruct! :xml, :version => "1.0" 
xml.rss :version => "2.0" do
  xml.channel do
    xml.title "Your Blog Title"
    xml.description "A blog about software and chocolate"
    xml.link posts_url

    for post in @posts
      xml.item do
        xml.title post.title
        xml.description post.content
        xml.pubDate post.date_posted.to_s(:rfc822)
        xml.link post_url(post)
        xml.guid post_url(post)
       end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我已将它添加到我的路线文件中, …

rss haml controller ruby-on-rails feed

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

angular $ watch和从数据库中检索数据的问题

我是一个新手编程试图将一个Web应用程序与Angular,node.js和图形数据库neo4j组合在一起.

我想根据用户选择(或拒绝)术语(点击按钮)动态加载数据库中的内容.每次单击按钮时,相关术语都会添加到数组中(排除或包含).这个想法是每次选择新术语时都会对数据库进行新的调用.

我现在停留在如何调用数据库来检索内容.我正在尝试使用$ watch观察阵列的变化.出现问题,我遇到了解决问题的麻烦.

这是控制器代码:

angular.module('myApp.controllers', []).    
controller('content',function($scope,$http, queryTerms, $watch){


    //watch arrays of terms for changes and fetch results based on what is selected
    $watch(function() { return angular.toJson( [ queryTerms.includedTerms, queryTerms.excludedTerms ] ) },
        function() {
            $http({
                method:'get',
                url:'/query/getContent',   
                params: {includeTerms:queryTerms.includedTerms , excludeTerms:queryTerms.excludedTerms}

            }).
            success(function(data){
                    //feed content data to display for viewing

            }).
            error(function(data){
                    $scope.test = "Error :("

            });
        });

});
Run Code Online (Sandbox Code Playgroud)

当我使用$ watch时,我收到以下错误:

Error: Unknown provider: $watchProvider <- $watch
Run Code Online (Sandbox Code Playgroud)

这是一种可怕的愚蠢方式来解决这个问题吗?任何建议都将非常感激 - 我正在学习,因为我到目前为止,我在这里得到的建议是惊人的.谢谢!

model-view-controller controller angularjs

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

捆绑不存在:Symfony2

我是Symfony2的新手.我无法弄清楚发生了什么.此代码(设置为测试是否可以检测到捆绑包):

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Ivory\GoogleMap\Overlays\Animation;
use Ivory\GoogleMap\Overlays\Marker;

class DefaultController extends Controller {

public function mapAction() {
    $map = $this->get ( 'ivory_google_map.map' );
        return $this->render ( 'KrewMediaLocalFarmBundle:Default:map.html.twig', array('map' => $map) );
  }
}
Run Code Online (Sandbox Code Playgroud)

工作,渲染一个简单的地图,而这个代码(涉及嵌入控制器以使用数据渲染地图的真实代码)

<?php
// localfarm/src/KrewMedia/Bundle/LocalFarmBundle/Controller/DefaultController.php
namespace KrewMedia\Bundle\LocalFarmBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Ivory\GoogleMap\Overlays\Animation;
use Ivory\GoogleMap\Overlays\Marker;

class DefaultController extends Controller {

public function mapAction() {
    //$map = $this->get ( 'ivory_google_map.map' );
        return $this->render ( 'KrewMediaLocalFarmBundle:Default:maptest.html.twig');
  }
}
Run Code Online (Sandbox Code Playgroud)

给我这个错误:"在渲染模板期间抛出异常("Bundle"LocalFarmBundle"不存在或者没有启用.也许你忘了将它添加到AppKernel.php文件的registerBundles()方法中?")在KrewMediaLocalFarmBundle中:默认:maptest.html.twig在第3行."

maptest.html.twig和map.html.twig都位于LocalFarmBundle中的同一文件夹中.我想知道为什么捆绑包在第一段代码中找到而在第二段没有.相关的路由是这样的

krew_media_local_farm_homepage:
    pattern:  /index
    defaults: { _controller: KrewMediaLocalFarmBundle:Default:index }

krew_media_local_farm_map:
     pattern:  /map
     defaults: { …
Run Code Online (Sandbox Code Playgroud)

bundle controller symfony

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

无法找到控制器的操作

通过Ruby on Rails教程学习; 我知道这很简单,但我很难过,因为我对这个框架很新.

在生成控制器并在.config日志中映射它并在我的控制器中定义一个动作之后 - 我仍然在localhost:3000中收到相同的"动作错误".

这是我得到的:

'未知行动'

'PostsController无法找到'new'动作

控制器:

class PostsController < ApplicationController
end

def new
end
Run Code Online (Sandbox Code Playgroud)

任何人都可以对这个问题有所了解吗?

controller ruby-on-rails-4

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

错误:在非对象上调用成员函数get()

我试图用Swift_Message发送邮件但是当我去发送它不会发送的数据时我得到一个错误

FatalErrorException:错误:在/vagrant/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php第252行中调用非对象上的成员函数get()

这是我正在使用的电子邮件控制器.

use Symfony\Component\Finder\Shell\Command;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;

class EmailController extends Controller{

    public function createMessage($subject, $from, $from_name, $to, $to_name, $body){
        // Create the message
        $message = \Swift_Message::newInstance()

            // Give the message a subject
            ->setSubject($subject)

            // Set the From address with an associative array
            ->setFrom(array($from => $from_name))

            // Set the To addresses with an associative array
            ->setTo(array($to => $to_name))

            // Give it a body
            ->setBody($body, 'text/html');

        return $message;
    }

    public function sendEmail($message, $urlAlias){

        $this->get('mailer')->send($message);

        return $this->redirect($this->generateUrl($urlAlias)); …
Run Code Online (Sandbox Code Playgroud)

php controller symfony

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