Keep update Symfony3 tips

My computer develop environment

1
2
3
4
5
"require": {
"php": ">=5.5.9",
"symfony/symfony": "3.2.*",
...
}

Doctrine ‘s column default value declares in annotation

options={"default":0}

1
2
3
4
5
6
/**
* @var int
*
* @ORM\Column(name="view", type="integer", nullable=true, options={"default":0})
*/
private $view;
  • This can not be set in command “php bin/console doctrine:generate:entity” , you should add this in entity class manually

Doctrine add column index

@ORM\Table(name="book",indexes={@ORM\Index(name="view_praise", columns={"view","praise"})})

1
2
3
4
5
6
7
8
9
10
/**
* Book
*
* @ORM\Table(name="book",indexes={@ORM\Index(name="view_praise", columns={"view","praise"})})
* @ORM\Entity(repositoryClass="AppBundle\Repository\BookRepository")
*/
class Book
{
...
}

Create Console Command

Create a Command directory under the bundle and the php script file suffixed with Command.php.

1
2
3
4
5
6
7
$ tree yourProject/src/
/yourProject/src/
└── AppBundle
├── AppBundle.php
├── Command
│   └── xxxxCommand.php
...

or entry this command

1
$ php bin/console generate:command

How to Logout

Add new config in Symfony firewall

1
$ vim app/config/security.ym

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
security:
providers:
webservice:
id: app.webservice_user_provider

firewalls:
...
main:
form_login:
login_path: login
check_path: login
use_referer: true

logout:
path: /logout
target: /

And new a controller method in your security provider

1
$ vim src/AppBundle/Controller/SecurityController.php

My provider app.webservice_user_provider direct to the class SecurityController.php

1
2
3
4
5
6
7
/**
* @Route("/logout", name="logout")
*/
public function logoutAction()
{
//don't need do anything
}

If composer install very slow even if the composer source has been changed

Check if php-zip is installed, if it is now installed, composer will download the source installation ans case it to be slow

1
$ sudo apt-get install php-zip

Get twig html content in controller

1
2
3
4
5
$response = $this->forward('AppBundle:Preview:articlePreviewContent', [
'articleId' => $articleId,
]);

$content = $response->getContent();

Maximum function nesting level of ‘256’ reached, aborting!

This problem will happen if you are using xdeubg.
You should change this limit in xdebug default configuration.

1
$ vim /etc/php5/conf.d/ext-xdebug.ini

Add this option and restart php service.

1
2
3
[xdebug]
...
xdebug.max_nesting_level = 1000

Argument 1 passed to Twig_Filter::__construct() must be an instance of string, string given

If you upgrade you php version from 5.6.9 to 7.0+
You should upgrade the twig version too.

1
2
//composer.json
"twig/twig": "~1.34",

Get all params in controller

If request is GET method

1
$arr = $request->query->all();

If request is POST method

1
$arr = $request->request->all();

Add twig error pages like 404, 403

Add the error page twig under this directory

1
2
3
4
5
6
$ tree -L 1 app/Resources/TwigBundle/views/Exception/
app/Resources/TwigBundle/views/Exception/
├── error.html.twig
├── error403.html.twig
├── error404.html.twig
└── error500.html.twig
Share