fos user bundle security validation

A login authentication would be handled by securitybundle in symfony.

But how about if you need to validate or authenticate username and password from controller?

Here is what you can do

        
        public function authenticatememberAction($user, $password)
        {
           $valid = false;

           $user_manager = $this->get('fos_user.user_manager');
           $factory = $this->get('security.encoder_factory');

           $user = $user_manager->findUserByUsername($user);//get the user first
           if (!empty($user)) {
              $encoder = $factory->getEncoder($user); //then the encoder
              $valid = ($encoder->isPasswordValid($user->getPassword(),$password,$user->getSalt())) ? true : false;
           }

           return new JsonResponse(['valid'=>$valid]);
        }

The above snippet will validate if the provided username password combination is correct or not.

taking off of the airplane

how to deploy symfony application to the production server

Deploy Symfony application to production – setting it live

This is a continuation of deploying symfony application to the server part one

Deploy Symfony application to production part two

4. Installing composer dependencies

Be in your symfony app first

cd /apps/symfony-app

The run the installer here

composer install --no-dev --optimize-autoloader

Under most circumstances, this will run without any problem. If there are any problems, composer will log them for you so follow those and solve those. And this will create a vendor folder in the folder you run composer. Continue reading how to deploy symfony application to the production server

airplane in he runway

Deploying Symfony on Production Server

Time to ship your symfony app to production? Here are some flight checks. Lets assume your symfony app is in folder symfony-app

1. Don’t put Symfony app in the public accessible folder on your server

If your servers public facing folder is, say, /var/www/html, then don’t put the whole symfony folder there.

Choose another deeper and non-public facing folder. Let say /apps/symfony-app. I will list how you would put the public facing folders later.
For this to happen run this command on your server

mkdir /apps

2. Pushing your code to production server

You can use FTP client like filezilla or cyberduck and put your code in /apps
If you are using git, then you can clone your app like this

cd /apps
git clone https://username@bitbucket.org/username/symfony-app.git symfony-app

Continue reading Deploying Symfony on Production Server

Screen Shot 2016-04-10 at 7.49.57 PM

composer install-update killed on vagrant machine

These are also other stuffs going on:
composer update or composer install takes forever and the command line responds as killed

Specially this might happen on ordinary symfony application.Screen Shot 2016-04-10 at 8.16.40 PM

This is mostly, very very likely, related to memory issue.

Solution 1: perform your composer update or install on the host machine if possible.

This might not be possible in some scenarios if there are dependencies that rely on installed packages.

But, if that is not the problem, have on the host and the files would be sync’d anyway.Screen Shot 2016-04-10 at 8.07.07 PM

Solution 2: increase the memory on the provision file you have. I am using puphpet so, for that all you would have to do would be

cd /path/to/puphpet/
vim comfig.yml

then search for memory which by default be 512 and increase it at least to 1024 or better 2048 and that should solve the problem

Tip

To see the how much memory you have on your vagrant machine, ssh into your machine and

vagrant ssh
free -h

the free command would tell you how much memory you have and how is used.

error = MongoId not found in symfony application

I got the MongoId not found error on the the application I recently moved to remote server.
The app is working fine locally and the problem appears to happen on the remote one only.
The problem seems obvious, and I checked the mongo version I have locally vs the version I have on the remote – they are different

I have newer version of mongo installed on the remote server. And the doctrine orm handling my mongo objects was also older.
I updated my doctrie orm on composer as

 "doctrine/orm": "2.4.6",

This took care of the problem – at least for now 😉

Deploying Symfony app on micro instance of aws – Amazon EC2

The major issue you would face is memory. Yeah, the instance comes with small memory allocation and anything memory intensive task might not work there. Your composer install command might not do anything as well.

The major one would be Composer install just hangs and even aborts the process
When you are issuing

composer install

It will eat a bit of memory and doing it on the instance might not work as expected.

Here are the methods I used to overcome it.

1. Incremental install
On you composer.json file, you can try to list only one or two packages at a time and issue composer update vendor/package and if your individual packages are small enough you might get away with it. In my case this didn’t work

2. Ship your vendor from your local machine to the instance
yeah, just have all your composer install where you will be comforted by memory in Gigabytes and just zip and ship it to your instance.
On your local machine

tar -cf vendor.tar.gz /path/to/vendor/folder

Once you have the tar or zip of any of your favorite compressed file

scp -i /path/to/your/pem/file vendor.tar.gz ec2-user@ec2-domain-goes-here:/path/on/instance

This is assuming this will transfer your vendor file to instance.
Then log into your instance and just uncompress the file and put it on the root directory of your application.
This would be just the half of the work.
Then you will need to generate the bootstrap cache file

composer run-script post-update-cmd

yeah.. this will take care of creating the cache file of the bootstrap along with other stuffs that you put on your composer.json post-update-cmd part.
Sometimes you might want to give the write access to the app/cache and app/logs folders as well

ENjOY

Using unquoted json formatted result set in symfony2 twig

In the request->process->response world of the MVC infrastructure, Symfony2 fits just perfect.

I was working on some data intensive site. All I want was to get the set of records from the mysql database and pass it to the front end through the controller. The front end wants the JSON format of the result set. The front end is getting the polished output of the twig

The problem
Front-end is not happy since it is getting quote-escaped version of JSON

The solution

Very very very simple. Rather than passing the json_encode(all_cars), where all_cars being the array of results coming from database or whatever it is, just pass the array it self

{{ all_cars | json_encode | raw }}

Yup, twig will not try to escape the quotes in this case since it is told to present raw.

Additional View

As you can see in this menu, both JSON and html views are together as combo. From design point of view this is not a good approach.

Better approach

You can bake the whole view right in the view and you might not want additional javascript logic on your view

OR
Have specific API to return 100% JSON response for the request like

some domain dot com/all/cars

By hitting this from, say your ajax call, you will be provided with json formatted list of all cars.

Since you are calling it from javascript, it will be directly coming to its home and no funny business would be there.
Then you will have another url call to load the page say:

some domain dot com/cars

where it will simply load the bare html format to the front-end and the front-end will know what to do with it.

Of course, this would have two trips and even more so it would be suitable for 80-90% of ajaxy sites..

EnJoY!

Check if the variable is defined in symfony twig

In twig, specially for the templates being used by many controllers, there might be variables set by one controller but not by others.

Well one way to address could be to have dummy data on each controller and either to check for that dummy value and do something or just to print it

But, symfony has a way to check if the variable has made it to the template or not

{% if some.variable is defined %} what a world {% endif %}

so the usage of defined can rescue us.

Fatal error: require(): Failed opening required Hydrator in mongodb

I was working on putting POPO to MongoDB in symfony project.
I have the Document inside BundleDocumentMyDocument.php

I want to create this document for the first time, but would like to update the document if it exists based on its two fields like:
* I have taken out some of additional information and focus on the main code only..

$file_id = getTheFileId();
$accessor_id = getAccessorId();
$repository=$this->getMongoService()
				->getManager()
				->getRepository('SomethinSomethingBundleDocumentFileAccess');
			$file_access=$repository->findOneBy(array('fileId'=>$file_id, 'accessorId'=>$accessor_id));
			if ($file_access && $file_access instanceof FileAccess){
				$file_access->setAccessCount($file_access->getAccessCount()+1);
			}else{
				$file_access=new FileAccess();
				$file_access->setFileId($file_id);
				$file_access->setAccessorId($accessor_id);
				$file_access->setAccessorType(FileAccess::ACCESS_TYPE_MEMBER);
				$file_access->setAccessDate(time());
				$file_access->setAccessCount(1);
			}
			$document_manager=$this->getMongoService()->getManager();
			$document_manager->persist($file_access);
			$document_manager->flush();

The problem I was facing was when I try to access the record, not when I was adding it.
and I know the record was there by using the command line mongo tool

The solution appears to be the missing config item for mongo:
on your config for mongo add

auto_generate_hydrator_classes: true

and that should fix the problem.