Skip to content

Instantly share code, notes, and snippets.

@tobalsan
tobalsan / symfony.conf
Created May 13, 2020 10:19
Symfony 4 Apache configuration
<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /var/www/project/public
DirectoryIndex /index.php
<Directory /var/www/project/public>
AllowOverride None
Order Allow,Deny
@tobalsan
tobalsan / howto.md
Created November 29, 2017 09:51
Exclude 404 (NotFoundHttpException) from Symfony logs

Add a custom activation strategy:

monolog:
    handlers:
        main:
            type:         fingers_crossed
            action_level: error
            handler:      nested
            activation_strategy: my.activation.strategy # service id
@tobalsan
tobalsan / sf2-security.php
Last active August 29, 2015 14:04
Symfony2: Manage user roles and authentication state
$token = $this->get('security.context')->getToken();
// do things that change user's roles
$token->setAuthenticated(false);
/* OR */
// Add new role
$user->addRole('ROLE_NEW');
// persist the user and it's new role to database
@tobalsan
tobalsan / sf2-user-role-refresh.php
Last active August 29, 2015 14:04
Symfony2: Custom equals method in user entity to refresh roles
<?php
// Just change the "equals" method of your User class like the following to enable automatic roles refresh instead of having to logout / login for roles change to take effect
class User implements UserInterface
{
// ...
/**
* Compares this user to another to determine if they are the same.
@tobalsan
tobalsan / sf2-constraints-validators.php
Last active August 29, 2015 14:04
Symfony2: customize constraints for validation groups
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: { fields: username, groups: [create_user, default], message: userExists }
- FOS\UserBundle\Validator\Unique: { property: usernameCanonical, groups: [create_user, default], message: userExists }
- Callback:
methods: [isTimeZoneValid]
groups: [create_user, default]
@tobalsan
tobalsan / js-image-preload.js
Last active August 29, 2015 14:04
JavaScript: Preload images at loading
// Prelod a single image
var image = $('<img />').attr('src', '/path/to/image.png');
// Multiple images
function preload(arrayOfImages) {
$(arrayOfImages).each(function(){
$('<img/>')[0].src = this;
// Alternatively you could use:
// (new Image()).src = this;
});
@tobalsan
tobalsan / sf1-4-query-to-array.php
Last active August 29, 2015 14:04
Symfony 1.4: Retrieve full query string into an array directly from template
<?php
// Retrieve full query string into an array directly from template
$params = $sf_request->getParameterHolder()->getAll()->getRawValue();
@tobalsan
tobalsan / git-set-upstream.sh
Last active August 29, 2015 14:04
Git: manually set upstream for a branch
// Git 1.8
git branch -u remote/foo
// If not in branch foo
git branch -u remote/foo foo
// Git 1.7
git branch --set-upstream foo remote/foo
@tobalsan
tobalsan / config.yml
Last active August 29, 2015 14:04
Symfony2: Allow nested parameters in configuration YAML
// app/config/config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
# various imports
- { resource: acmehello.yml }
// app/config/acmehello.yml
acme_hello:
param:
<?php
# src/Acme/HelloBundle/Twig/AcmeExtension.php
namespace Acme\HelloBundle\Twig;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Locale\Locale;
class AcmeExtension extends \Twig_Extension
{