Cannot redeclare geoip_country_code_by_name

Today I had this error on my Ubuntu Linux box

Fatal error: Cannot redeclare geoip_country_code_by_name()

The reason was that I have geoip extension enabled in my PHP configuration and also I used the geoip.inc file from maxmind.com.

The solution is very simple. Actually, there are two solutions:

First is to disable the geoip extension from your configuration. In /etc/php5/apache2/conf.d/geoip.ini comment the first line:

;extension=geoip.so

The second solution: in your geoip.inc file, look for this code

function geoip_country_code_by_name($gi, $name) {
...
}
 
function geoip_country_name_by_name($gi, $name) {
...
}

and replace with

if (!function_exists('geoip_country_code_by_name')) {
  function geoip_country_code_by_name($gi, $name) {
  ...
  }
}
 
if (!function_exists('geoip_country_name_by_name')) {
  function geoip_country_name_by_name($gi, $name) {
  ...
  }
}

 

 

How To – Activate PHP in public_html for Ubuntu 10

It seems it is a bug (I didn’t investigate too much) in Ubuntu 10.x related to PHP files in userdirs.

On my box I have Ubuntu 10.10, Apache/2.2.16 (Ubuntu) and PHP 5.3.3-1. Everything worked fine except the PHP files from ~userdir. Instead to parse these files, the web server send them directly to the browser which open the “save as” window.

The problem is in /etc/apache2/mods-enabled/php5.conf file.

<IfModule mod_userdir.c>
  <Directory /home/*/public_html>
     php_admin_value engine Off
  </Directory>
</IfModule>

These lines should be commented:

#<IfModule mod_userdir.c>
#  <Directory /home/*/public_html>
#     php_admin_value engine Off
#  </Directory>
#</IfModule>

 

A simple MVC framework

will describe in the following post a (very) simple MVC framework. You can download the source code from here or can make a copy from bitbucket repository.

As you can see, we have the following folders:
– web: here resides your public files (images, css, and index.php)
– app: here you can find application files: models, views, controllers (MVC)

How it works?
URL:
a) http://www.mydomain.com/testfolder/web/index.php
b) http://www.mydomain.com/testfolder/web/index.php/index/index/name/razvan…

All the requests goes to index.php from web folder. So you can set up your webserver and .htaccess for this.

The first request is to index.php from web folder.

  • Here we include all necesary files (of course we can use autoload, but this is a proof of concept example).
  • Call for an instance of FrontController class
  • Call the route method of FrontController class
  • Display the result (body).

FrontController class (located in front.php file) implements the singleton pattern.

I will explain a little the constructor method and the route method. The other ones are obvious.

The constructor tries to match the controller name, the action name, and the parameters from the request URL.

In the class you can specify a start folder (START_FOLDER). In our example should be: START_FOLDER = ‘/testfolder/web/’
If no controller, and/or no action are specified in the request URL it assume is “index” (case a).

In route method we are using Reflection class to determine if indeed the class for the found controller exists

if(class_exists($this->getController()))

and is has the specified action

if($rc->hasMethod($this->getAction()))

Also, the controller must implement the interface IController:

if($rc->implementsInterface('IController'))

If all these conditions are valid, we can call the method

$method->invoke($controller);

 

In the next step, we are going to index controller class (file index.php from controllers). Here we have the “index” action.

– From the FrontController class we call getParams() method.
– Create a new View object
– Assign parameters to $view object properties
– Call render method of the $view object
– set content with setBody() front controller method.

The View class needs also some explications.

We overloaded the __set method so we can to assign parameters to $view object properties without they exists before.

We also wrote a __get method to return an empty string if you can to access a nonexistent property of a view object.
The render method, simply include the specified file using output buffering.

In the view file (index.php from views folder) you can access previously assigned properties with $this->propertyname.

This MVC framework is mainly based on the example from the book “Pro PHP Patterns, Frameworks, Testing and More” by Kevin McArthur.

 

Singleton Pattern

This pattern is probably one of the most simple and used pattern.

As php manual says: The Singleton pattern applies to situations in which there needs to be a single instance of a class. The most common example of this is a database connection. Implementing this pattern allows a programmer to make this single instance easily accessible by many other objects.

There are tons of information about this pattern over the Internet so I let you google for it.

You can find a singleton implementation in Simple MVC framework example.

How to create a command (task) in Symfony 2

This tutorial is deprecated. It was written when Symfony2 was in beta. 

In Symfony 1.x a task is a command line tool to help perform tasks around the application. The same is in Symfony 2. Unfortunately in Symfony2 there is no tool to auto-generate some code for these. To create a Symfony2 Command you must to have or to create in your Bundle a folder named Command. Here, you must create a file named MytaskCommand.php with the following code:

 

<?php
namespace ApplicationVremeaBundleCommand;

use SymfonyBundleFrameworkBundleCommandCommand;
use SymfonyComponentConsoleInputInputDefinition;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use SymfonyComponentRazvanUtilsCurl;

class ImportCommand extends Command {

 const NEWLINE = true;
 private $connection;

 protected function configure() {
 parent::configure();

 $this
                ->setName('tudorica:razvan')
                ->addArgument('type', InputArgument::REQUIRED, 'Type')
                ->addOption('country', 'c', InputOption::VALUE_OPTIONAL, 'Country', '')

        ;
    }

    /**
     * Executes the current command.
     *
     * @param InputInterface  $input  An InputInterface instance
     * @param OutputInterface $output An OutputInterface instance
     *
     * @return integer 0 if everything went fine, or an error code
     *
     * @throws LogicException When this abstract class is not implemented
     */
    protected function execute(InputInterface $input, OutputInterface $output) {
        // your code here.
        //if you want to access database (must to setup doctrine.dbal)
        $this->connection = $this->container->get('database_connection');

         //to get the user input argument "type" for this command
        $type = $input->getArgument('type');        

        //to get use option "contry" for this command
        $country = $input->getOption('country');

        //to write a message (in symfony1.x was named log).
        $output->write('type: '.$type, true);
    }

}

N

ow, you can open a console and go to app folder. If you write:

./console

you will see something like

router
  :debug                       Displays current routes for an application
  :dump-apache                 Dumps all routes as Apache rewrite rules
tudorica
  :razvan

You can execute this Command with: ./console tudorica:razvan