小编ed2*_*209的帖子

如何在PHP中定义一个空对象

使用新数组我这样做:

$aVal = array();

$aVal[key1][var1] = "something";
$aVal[key1][var2] = "something else";
Run Code Online (Sandbox Code Playgroud)

对象是否有类似的语法

(object)$oVal = "";

$oVal->key1->var1 = "something";
$oVal->key1->var2 = "something else";
Run Code Online (Sandbox Code Playgroud)

php object

357
推荐指数
12
解决办法
34万
查看次数

如何[递归]在PHP中压缩目录?

目录是这样的:

home/
    file1.html
    file2.html
Another_Dir/
    file8.html
    Sub_Dir/
        file19.html
Run Code Online (Sandbox Code Playgroud)

我使用PHPMyAdmin中使用的相同PHP Zip类http://trac.seagullproject.org/browser/branches/0.6-bugfix/lib/other/Zip.php.我不确定如何压缩目录而不仅仅是文件.这是我到目前为止所拥有的:

$aFiles = $this->da->getDirTree($target);
/* $aFiles is something like, path => filetime
Array
(
    [home] => 
    [home/file1.html] => 1251280379
    [home/file2.html] => 1251280377
    etc...
)

*/
$zip = & new Zip();
foreach( $aFiles as $fileLocation => $time ){
    $file = $target . "/" . $fileLocation;
    if ( is_file($file) ){
        $buffer = file_get_contents($file);
        $zip->addFile($buffer, $fileLocation);
    }
}
THEN_SOME_PHP_CLASS::toDownloadData($zip); // this bit works ok
Run Code Online (Sandbox Code Playgroud)

但是当我尝试解压缩相应的下载zip文件时,我得到"不允许操作"

这个错误只发生在我尝试解压缩我的mac时,当我通过命令行解压缩文件解压缩时.我是否需要在下载时发送特定的内容类型,目前为'application/zip'

php directory recursion zip directory-structure

117
推荐指数
3
解决办法
10万
查看次数

访问模板中的登录用户

我正在使用FOSuserbundle开始用户注册https://github.com/FriendsOfSymfony/FOSUserBundle

我已经注册/登录和注销了.我现在要做的是获取登录用户数据并将其呈现在我网站的每个页面上.像标题类型的东西中的"Hi username".

似乎在我的app/Resources/views/base.html.twig中嵌入一​​个控制器是执行此操作的最佳方式http://symfony.com/doc/current/book/templating.html#embedding-controllers

所以我编写了我的控制器来访问用户配置文件数据.我无法弄清楚如何在我的嵌入式控制器中访问FOS方法.所以从我的Acme/UserBundle/Controller/UserController.php我想这样做:

public function showAction()
{
    $user = $this->container->get('security.context')->getToken()->getUser();
    if (!is_object($user) || !$user instanceof UserInterface) {
        throw new AccessDeniedException(
               'This user does not have access to this section.');
    }

    return $this->container->get('templating')
      ->renderResponse('FOSUserBundle:Profile:show.html.'.$this->container
      ->getParameter('fos_user.template.engine'), array('user' => $user));
}
Run Code Online (Sandbox Code Playgroud)

我从中获取:vendor/bundles/FOS/UserBundle/Controller/ProfileController.php

symfony twig fosuserbundle

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

检查Symfony2中{{path()}}是否为当前{{path()}}

如何检查当前页面是否为此路径:

{{ path('someNamePath') }}
Run Code Online (Sandbox Code Playgroud)

我想为<a>元素设置一个css类,或者可以完全删除它,例如

{% if  isCurrentPath('someNamePath') %}
    <a href="{{ path('someNamePath') }}" class="YouAreHere">My Link</a>
{% else %}
    <a href="{{ path('someNamePath') }}">My Link</a>
{% endif %}
Run Code Online (Sandbox Code Playgroud)

php symfony twig

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

在Symfony2中将表单创建为服务

我希望能从任何控制器做的是:

$register = $this->get('register_manager');
return $this->render(
    'AcmeUserBundle:Account:register.html.twig',
     array(
         'form' => $register->getRegistrationForm(),
         )
 );
Run Code Online (Sandbox Code Playgroud)

在我的模板中

<form>
    {{ form_widget(form) }}
</form>
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止的设置方式

在我的Acme/UserBundle/Resources/config/services.yml

parameters:
    register_manager.class: Acme\UserBundle\Manager\RegisterManager

services:
    register_manager:
        class:     %register_manager.class%
        arguments: [@form.factory]
Run Code Online (Sandbox Code Playgroud)

RegisterManager.php我有

namespace Acme\UserBundle\Manager;

use Acme\UserBundle\Form\Type\RegistrationType;
use Symfony\Component\Form\FormFactoryInterface;

class RegisterManager
{

    protected $formFactory;

    public function __construct(FormFactoryInterface $formFactory)
    {
        $this->formFactory = $formFactory;
    }


    public function getRegistrationForm()
    {
        return $this->formFactory->createBuilder(new RegistrationType());
    }
}
Run Code Online (Sandbox Code Playgroud)

Acme\UserBundle\Form\Type\RegistrationType我有:

namespace Acme\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class RegistrationType extends AbstractType
{
    public function …
Run Code Online (Sandbox Code Playgroud)

php symfony

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

在树枝中使用自定义功能

在我的模板中,我想输出服务器时区.

我的模板有类似的东西

{{ getservertimezone }}
Run Code Online (Sandbox Code Playgroud)

然后在我所拥有的捆绑的services.yml配置中

my.twig.extension:
    class: My\WebsiteBundle\Extensions\Twig\SomeTemplateHelper
    tags:
           - { name: twig.extension }
Run Code Online (Sandbox Code Playgroud)

我的SomeTemplateHelper样子

namespace My\WebsiteBundle\Extensions\Twig;

class SomeTemplateHelper extends \Twig_Extension
{

    public function getFilters() 
    {
        return array(
            'getservertimezone'  => new \Twig_Filter_Method($this, 'getServerTimeZone'),
        );
    }


    public function getServerTimeZone()
    {
        if (date_default_timezone_get()) {
            return date_default_timezone_get();
        } else if (ini_get('date.timezone')) {
            return ini_get('date.timezone');
        } else {
            return false;
        }
    }

    public function getName()
    {
        return 'some_helper';
    }

}
Run Code Online (Sandbox Code Playgroud)

但我不能称之为这种方法,除非它像过滤器那样使用{{ someval | getservertimezone }},有没有办法直接{{ getservertimezone() }}调用?

php symfony twig

24
推荐指数
3
解决办法
4万
查看次数

如何将树枝渲染模板作为JSON响应的一部分返回?

我想返回一个HTML片段以及json字符串中的其他值,这是我所拥有的:

$html = $this->render('sometemplate.html.twig', array( 'somevar' => $somevar ) );
$response = new Response(json_encode( array("html" => $html, "name" => "Joe Bloggs") ));
$response->headers->set('Content-Type', 'application/json');
return $response;
Run Code Online (Sandbox Code Playgroud)

但我得到的只是{"html":{"headers":{}}}.有没有办法只抓取渲染的HTML?

php json symfony twig

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

在Symfony2中注入Twig作为服务

我不想扩展标准控制器,而是将Twig注入我的一个类中.

控制器:

namespace Project\SomeBundle\Controller;

use Twig_Environment as Environment;

class SomeController
{
    private $twig;

    public function __construct( Environment $twig )
    {
        $this->twig    = $twig;
    }

    public function indexAction()
    {
        return $this->twig->render(
            'SomeBundle::template.html.twig', array()
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在services.yml我有以下内容:

project.controller.some:
    class: Project\SomeBundle\Controller\SomeController
    arguments: [ @twig ]
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

SomeController :: __ construct()必须是Twig_Environment的一个实例,没有给出

但我正在@twig通过config.我看不出我做错了什么.

编辑:

添加正确的代码 - 这是解决问题的原因:

// in `routing.yml` refer to the service you defined in `services.yml` 
project.controller.some
    project_website_home:
        pattern:  /
        defaults: { _controller: project.controller.some:index }
Run Code Online (Sandbox Code Playgroud)

dependency-injection symfony

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

将变量传递给Symfony2中的Assetic资产URL

有没有办法将变量传递给模板中的Assetic方法

{% stylesheets
    '@SomeExampleBundle/Resources/views/ SOMEVAR /css/*'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
Run Code Online (Sandbox Code Playgroud)

所以我想做的是SOMEVAR从控制器传递.

php symfony assetic

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

DoctrineMongoDBBundle在Symfony2中出现致命错误

我按照这里的指示:http: //symfony.com/doc/2.0/bundles/DoctrineMongoDBBundle/index.html

我安装了deps:

#deps
[doctrine-mongodb]
    git=http://github.com/doctrine/mongodb.git

[doctrine-mongodb-odm]
    git=http://github.com/doctrine/mongodb-odm.git

[DoctrineMongoDBBundle]
    git=http://github.com/symfony/DoctrineMongoDBBundle.git
    target=/bundles/Symfony/Bundle/DoctrineMongoDBBundle
Run Code Online (Sandbox Code Playgroud)

然后呢 $ php bin/vendors install

我添加了以下内容:

# app/autoload.php
$loader->registerNamespaces(array(
    // ....
    'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
    'Doctrine\\DBAL'   => __DIR__.'/../vendor/doctrine-dbal/lib',
    'Doctrine\\ODM\\MongoDB'    => __DIR__.'/../vendor/doctrine-mongodb-odm/lib',
    'Doctrine\\MongoDB'         => __DIR__.'/../vendor/doctrine-mongodb/lib',
    'Doctrine'         => __DIR__.'/../vendor/doctrine/lib',
    // ....
));

AnnotationRegistry::registerFile(
    __DIR__.'/../vendor/doctrine/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'
);
AnnotationRegistry::registerFile(
    __DIR__.'/../vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/DoctrineAnnotations.php'
);
Run Code Online (Sandbox Code Playgroud)

我还补充道

# app/AppKernel.php
$bundles = array(
    //....
    new Symfony\Bundle\DoctrineMongoDBBundle\DoctrineMongoDBBundle(),
    //....
);
Run Code Online (Sandbox Code Playgroud)

我的学说配置如下:

# Doctrine Configuration
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        charset:  UTF8

    orm: …
Run Code Online (Sandbox Code Playgroud)

php mongodb symfony doctrine-orm symfony-2.0

11
推荐指数
1
解决办法
4801
查看次数