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.

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

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.

bundle does not contain any mapped documents/entities

This will be an error you would see when trying to generate entities or documents using:

app/console doctrine:mongodb:generate:documents YourPathToBundle

Actually the error would also suggest if you have mapping annotation error.

Mostly this could arise from mapping error as suggested.. check if you have @MongoDbDocument in the case of document or the respective @ORMEntity that would tell what that Plain Old Php Object is representing..
btw, the MongoDb and ORM are the aliases, so it could different on your setting..

the requested PHP extension mongo is missing from your system.

Got this error when trying to add the mongodb stuff your php project – specially using composer then here is how to solve it.

The first thing is you have to add the mongodb driver to php.

You might need PEAR on your system. Adding pear to your system is relatively easy and google can help on that.

Then do the following on your terminal

sudo pecl install mongo
pecl install mongo

The last lines of successful installation would tell where the installation has occurred and some more important information.

Now you need to add the mongo.so to the php.ini

if you don’t know where you have php.ini run (on ubuntu)

locate php.ini

then run

echo "extension=mongo.so" >> path-to-php.ini