понедельник, 19 января 2015 г.

Symfony2 Forms and Doctrine Entity: Unique field values validation

Symfony2 Forms and Doctrine Entity: Unique field values validation

If you’re using Symfony2 forms paired with Doctrine entities, you might sometimes need to validate fields so that the values in that field is always unique. Say – “username” field or “email” field. Two users must not have the same username or email.
There is a form validation constraint – “Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity” which integrates nicely with your entity and allows unique values validation when the form is submitted. Let’s look at some code samples to see how it works:
Here, the email and username fields will be checked for duplicate values and the defined message will be displayed if duplicate values are found.

пятница, 16 января 2015 г.

Symfony2 Nginx and Apache2

Nginx Conf

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /PATH_TOSYMFONY_PROJECT/web;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
            try_files $uri /app.php$uri$is_args$query_string;
        }

        #
        # Pass (app|app_dev|config).php to apache2
        #
        location ~ ^/(app|app_dev|config)\.php(/|$) {
                #DMZ Only
                #Default Port
                set $_port 80;
                if ($host = EXTERNAL_IP) {
                    set $_port 10880;
                }

                if ($host = INTERNAL_IP) {
                    set $_port 80;
                }
                #End For DMZ
                proxy_pass          http://127.0.0.1:81;
                proxy_set_header    X-Real-IP  $remote_addr;

                proxy_set_header    X-Forwarded-For $remote_addr;
                proxy_set_header    X-Forwarded-Proto $scheme;

                #
                # Нужно для того чтобы симфони могло генерировать урлы без "app.php"
                #
                proxy_set_header    X-Rewrite-URL $request_uri;
                proxy_set_header    X-Original-URL $request_uri;
                proxy_set_header    X-Original-Addr $remote_addr;

                proxy_set_header    Host $host:$_port;

        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }
}


Apache2 conf

<VirtualHost *:81>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/PATH_TOSYMFONY_PROJECT/web"
    DirectoryIndex app.php

    <IfModule env_module>
        SetEnv APPLICATION_ENV development
    </IfModule>

    <Directory "/PATH_TOSYMFONY_PROJECT/web">
        AllowOverride None
        Allow from All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

среда, 14 января 2015 г.

Symfony Permissions

One common issue when installing Symfony is that the app/cache and app/logsdirectories must be writable both by the web server and the command line user. On a UNIX system, if your web server user is different from your command line user, you can try one of the following solutions.
1. Use the same user for the CLI and the web server
In development environments, it is a common practice to use the same UNIX user for the CLI and the web server because it avoids any of these permissions issues when setting up new projects. This can be done by editing your web server configuration (e.g. commonly httpd.conf or apache2.conf for Apache) and setting its user to be the same as your CLI user (e.g. for Apache, update the User and Group values).
2. Using ACL on a system that supports chmod +a
Many systems allow you to use the chmod +a command. Try this first, and if you get an error - try the next method. This uses a command to try to determine your web server user and set it as HTTPDUSER:
1
2
3
4
5
6
$ rm -rf app/cache/*
$ rm -rf app/logs/*

$ HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1`
$ sudo chmod +a "$HTTPDUSER allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs
$ sudo chmod +a "`whoami` allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs
3. Using ACL on a system that does not support chmod +a
Some systems don't support chmod +a, but do support another utility called setfacl. You may need to enable ACL support on your partition and install setfacl before using it (as is the case with Ubuntu). This uses a command to try to determine your web server user and set it as HTTPDUSER:
1
2
3
$ HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1`
$ sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
$ sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
If this doesn't work, try adding -n option.
4. Without using ACL
If none of the previous methods work for you, change the umask so that the cache and log directories will be group-writable or world-writable (depending if the web server user and the command line user are in the same group or not). To achieve this, put the following line at the beginning of the app/consoleweb/app.php and web/app_dev.phpfiles:
1
2
3
4
5
umask(0002); // This will let the permissions be 0775

// or

umask(0000); // This will let the permissions be 0777
Note that using the ACL is recommended when you have access to them on your server because changing the umask is not thread-safe.