我正在使用Symfony 2开发一个项目,我已经构建了一个bundle来处理我的所有数据库服务,它们传递JSON数据.
我的问题/问题:
可以发布一个直接的JSON对象吗?目前我正在欺骗我的ajax调用的正常表单帖子,json={"key":"value"}
如果我不给它一个名字给对象一个名字我似乎无法从Symfony请求对象获取数据$JSON = $request->request->get('json');
我希望能够使用一个服务包来处理来自AJAX调用的数据或正常的Symfony表单.目前我正在提交提交的Symfony表单,然后使用JSON_ENCODE获取数据,我只是无法解决如何将数据发布到我期望请求数据的服务控制器.
总结一下:
我希望Symfony接受JSON post对象而不是表单.
我想使用Request/Response在控制器之间传递JSON对象
如果我说这一切都错了,请随时告诉我!
我不得不更新我的.htaccess:
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)
对此:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)
让它在AWS Elasic负载均衡器后面工作.
这一切似乎在AWS上运行良好,但在我的本地环境中,我陷入了重定向循环.
如何让这个设置在两种环境中正常工作?
出于某种原因,当我发送一个新的$ client->请求时,我指定的标头会丢失:
public function testGetClientsAction()
{
$client = static::createClient();
$cookie = new Cookie('locale2', 'en', time() + 3600 * 24 * 7, '/', null, false, false);
$client->getCookieJar()->set($cookie);
// Visit user login page and login
$crawler = $client->request('GET', '/login');
$form = $crawler->selectButton('login')->form();
$crawler = $client->submit($form, array('_username' => 'greg', '_password' => 'greg'));
$client->request(
'GET',
'/clients',
array(),
array(),
array('X-Requested-With' => 'XMLHttpRequest', 'accept' => 'application/json')
);
print_r($client->getResponse());
die();
Run Code Online (Sandbox Code Playgroud)
}
在正在测试的方法中,我在第一行有这个:
print_r($request->headers->all());
Run Code Online (Sandbox Code Playgroud)
答复如下:
Array
(
[host] => Array
(
[0] => localhost
)
[user-agent] => Array …
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个非常标准的单元测试,我调用一个方法并断言它的响应,但是我正在测试的方法在同一个类中调用另一个方法,它会做一些繁重的工作.
我想模拟那个方法,但仍然执行我正在测试的方法,只有通过调用另一个方法返回的模拟值.
我愚弄了这个例子,让它变得尽可能简单.
class MyClass
{
// I want to test this method, but mock the handleValue method to always return a set value.
public function testMethod($arg)
{
$value = $arg->getValue();
$this->handleValue($value);
}
// This method needs to be mocked to always return a set value.
public function handleValue($value)
{
// Do a bunch of stuff...
$value += 20;
return $value;
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试写测试.
class MyClassTest extends \PHPUnit_Framework_TestCase
{
public function testTheTestMethod()
{
// mock the object that is passed …
Run Code Online (Sandbox Code Playgroud) 这个问题似乎已被多次询问,但没有一个解决方案适合我.
我在我的生产环境中,这就是我所做的:
还有什么我想念的吗?从php app/console assetic:dump --env=prod --no-debug
文件名中匹配的文件名与匹配路径内容相匹配,文件生成完全正常
.
每当我尝试清除控制台上的缓存时,我都会收到以下错误:
[Symfony\Component\DependencyInjection\Exception\InactiveScopeException]
You cannot create a service ("request") of an inactive scope ("request").
Run Code Online (Sandbox Code Playgroud)
有谁之前经历过这个吗?谢谢.
编辑:代码示例:
//accessing request object
namespace Greg\TestBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerInterface;
use HvH\APIschemaBundle\Controller\Validation;
use HvH\APIschemaBundle\Controller\Helpers;
//FOSRestBundle
use FOS\RestBundle\View\View;
class TestController extends Controller
{
public function testAction(Request $request)
{
//get any query strings
$query_strings = $request->query->all();
return($query_strings);
}
}
Run Code Online (Sandbox Code Playgroud)
XML 不确定您要查找哪个文件...
我安装了Mountain Lion,我无法让phpunit工作.
$ pear config-get bin_dir
/Users/greg/pear/bin
$cd /Users/greg/pear/bin
$ls
pear*
peardev*
pecl*
phpunit*
$phpunit
-bash: phpunit: command not found
Run Code Online (Sandbox Code Playgroud) 我有一个表格,其中每行的最后一列包含一个小的加载图标,我想在单击表格内的按钮时显示该图标.
当使用ng-repeat生成每个表行时,加载器将显示在每一行而不是单独的一行中.如何仅针对当前单击的索引将ng-show设置为true或false?
模板:
<tr ng-repeat="record in records">
<td>{{ record.name }}</td>
<td><a ng-click="someAction(record.name)">Some Action</a></td>
<td ng-show="loading">Loading...</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
控制器:
$scope.someAction = function(recordName) {
$scope.loading = true;
};
Run Code Online (Sandbox Code Playgroud) 我正在尝试设置我的功能测试,我遇到了经过身份验证的问题.我已经阅读了这本指南:http://symfony.com/doc/current/cookbook/testing/http_authentication.html并实现了他们所说的要做但我仍然坚持重定向登录.我确信这是微不足道的,但我不确定是什么.
测试控制器
namespace HvH\ClientsBundle\Tests\Controller;
use HvH\ClientsBundle\Controller\ClientsController;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\Session;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ClientsControllerTest extends WebTestCase
{
public function testGetClientsAction()
{
$client = static::createClient();
$client->request(
'/clients/123456',
'GET',
array(), /* request params */
array(), /* files */
array('X-Requested-With' => "XMLHttpRequest", 'PHP_AUTH_USER' => 'testuser', 'PHP_AUTH_PW' => 'testpass')
);
print_r($client->getResponse());
die();
}
}
Run Code Online (Sandbox Code Playgroud)
congif_test.yml
security:
firewalls:
secured_area:
http_basic:
Run Code Online (Sandbox Code Playgroud)
请求的结果
Symfony\Component\HttpFoundation\RedirectResponse Object
(
[headers] => Symfony\Component\HttpFoundation\ResponseHeaderBag Object
(
[computedCacheControl:protected] => Array
(
[no-cache] => 1
) …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在Symfony 2.1中使用composer从github库中删除供应商,这些库不是作曲家意识的,也可能永远不会.
例如:Old Deps文件:
[jQuery]
git=git://github.com/jquery/jquery.git
version=1.8.1
[Mocha]
git=https://github.com/visionmedia/mocha.git
Run Code Online (Sandbox Code Playgroud)
作曲家(不起作用)
"repositories": [
{
"type": "package",
"package": {
"name": "jquery",
"version": "1.8.1",
"dist": {
"url": "git://github.com/jquery/jquery.git",
"type": "git"
}
}
}
],
"require": {
"jquery": "1.8.1"
}
Run Code Online (Sandbox Code Playgroud) symfony ×6
phpunit ×4
php ×2
.htaccess ×1
ajax ×1
angularjs ×1
apache ×1
assetic ×1
composer-php ×1
javascript ×1
json ×1
mod-rewrite ×1
pear ×1
redirect ×1
symfony-2.1 ×1