Why should I choose PHP over ROR for my web application?

First of let me clear one thing that i have not much experience of writing ROR apps, nor i am favouring php as i have a some experience in it.
Simplicity:

Talking about php language it is simple and easy to learn who knows c or other programming language.

Ruby is used in ROR. But it is not as simple as PHP.

Dev. Environmnet:

To getting started with php you just haveto set up a local web server with php integrated like xampp or wamp and start developing with php in few minutes.

Wel with ROR you have to do a lot of excercise to setup working environment in your machine unless you use RailsInstalle.

Deployment:

The way we deploy php application is extremley makes me happy while working with php. I just change the code right away on the server and saved and my change in production. The biggest advantage is you can host your php application on shared host as well which is cheap.

Well to deploy ROR app you at least have vps pr it’s dedicated server like passenger. You must have to install all gems . So you have to manage your server too.
Frameworks in Php:

RoR itself a framework in ruby which is awesome for rapid developement. In php there are numbers of framework you will find but i prefer laravel due to simplicity and it’s features.

I can say php is a lnguage where you can solve problems in a way that you know the logic. 

Use laravel blade template out side of laravel

Php is a very nice platform and programming langauge for building web application. To build web application we have to build dynamic html using php tags and have to process data. But presenting html is a different construct and processing data is a different construct. So if we would like to separete these both construct in php we have to use template system. Laravel provides awesome template engine but if we do not have to use laravel but it’s blade template engine we can.

We just have to add below dependency in our project composer.json file

Link for laravel blade template php packe: https://packagist.org/packages/philo/laravel-blade

"require":
      {
        "philo/laravel-blade": "3.*"
      },

Than we have to install it via below command

$ > composer install 
or 
$ > composerupdate

Once php package for blade template engine is installed let’s see how we can use it.

Create cache and view folders inside your app directory.

Now let’s create one example template view file inside views directory.

Let’s use this template to show data pass from php.

This is how we can use an awesome laravel blade template engine out side of laravel in php project.

Convert HTML and CSS page to PDF and send in email attachement in PHP

I am mainting one web based product in php. Application is about hotel management for tariff calculation and all. My client has requested to email invoice as pdf generated by php based software.

I have divided process in two steps.

  1. Dynamically Generate PDF file some where on the server using html css produces by php
  2. Attach  this generated pdf invoice in email

First of all I did tried using dompdf famous php library to generate HTML css invoice but due to incompatibility with tables i did not get accurate invoice pdf as i want. So i started searching on google and also posted on google+. Finally from my google+ php community post i came across wkhtmltopdf which is really nice tool and solve my problem.

Wkhtmltopdf

Wkhtmltopdf is a command line utility that has nothing to do with php. But we can call this utility to generate pdf file from html.

Continue reading “Convert HTML and CSS page to PDF and send in email attachement in PHP”

Some cool Array functions in PHP

When ever you have an array and you have to find an element or filter an array for different situation most of the time this is achieved by writing loop. Some times its okay to write loop. But php offers some nice set of array functions which can help you to achieve your goal without writing a loop.

Let say we have an array for a list of products.

If we want to increment each product price by 10%, we can achieve this by using array_map function.

array map

you can do var_dump new product price have 10% more than previous one.

If i have to look for an item having category is mobile, array_filter can help here

array_filter

If i would like to sum all products price array_reduce work out of box.
array_reduce

In array_reduce function when callback function iterate over first item, $current_iteration_value is 0.

array_chunk
array_chunk is a function which i like most, i achieved pagination using this function.
It split array in to number of chunks.

Let say if we have numbers of products to display, where each page have only 22 products to display and whole products array have 500 products. You can achieve this by array_chunk

There are other good array_functions available in php which you can see here.

HTML element and their states

Designing html using css is nice and organized way to work. After working since last 5 years with web technologies i have learnt a lot many things for web development. The tools available today and the techniques used to develop applications. Among all i find HTML is interesting technology and very easy. In fact non-progarmmer can also learn it quickly and use it.

In this post i am talking about different state of HTML element. So when ever you design HTML page you have visibilty of it. Most of the HTML element have below states.

  • Normal State: Default state,

All of the element have this state and it is normal state in which we are desiging.

  • Hover State:  when mouse moves  over the element

All element have this state also.

css class :    :hove

  • Focus State: When keyboard cursor blinks in text box

Used for input elements when they receive keyboard focus.

css class :    :focus

  • Active State:

Used for hyperlink and button when they clicked

css class :    :active

Example:

Let see practial example of above 4 state. To understand above 4 state correctly we can take example of input type text.

1. Focus class example:

You can see that when you click on textbox and as soon as it receivs keyboard focus it’s border gets changed because we have added this behaviour in css using :focus css class.

2. hover class example

You can see when you mouse hover over the link , link change it’s background and underline is removed from link. This behaviour is achieved using :hover cssclass. You can use hover for any html element.

Note: this article is in progress.

Traits: Give power to your class

While building application using OOP class, interface play an important role. These two construct are very basic building block of software design. Unlike interface php has new concept called trait.

Trait is nothing new but set of class methods which has implementation details also. But interface have only function declaration not implementation. This makes PHP trait unique from interface.

Let’s take a practial example to understand this awesome feature provided by PHP 5.4 .

See below code which gives small higlight on how to use trait.

You can see that we have used one DriverTrait and used it in multiple class to leaverage same operaitons.

NOTE: This feature is only provided on PHP5.4 and above.

 

Building Own Framework in PHP Part-1

Up-til now i am very much fan of famous PHP framework Laravel. The latest version of it 5.1 has very nice features to build web application using modern tool. It provides full-stack approach to build web application. After using Laravel from very long time i am very much familiar with it’s component.

But from last couple of weeks i was thinking to build own small framework which is completely written by my self and fulfil requirements to build web application in day to day life. In Laravel i feel that entry point is Routes and it is very good place to control application routes and flow. So i started thinking to build own router. I have searched on google and found few results. Among all result i like slim framework Route class and got inspiration from there and try to build own small router for specific actions only.

My Small own router.

We can also pass arguments in dynamic action method in call_user_func_array function. You can see i have passed empty array to keep this simple. But if required we can pass argument and can expect that argument in controller method.

Like

$action = "getOrderDetail";
call_user_func_array(array($orderController, $action), array(34));

that we have to write getOrderDetail method as follow

    public function getOrderDetail($order_id)
	{
		echo "Now you will receive order detail for order id :".$order_id;
	}

From the above code i can now have some confidence to build this router component generic using .htaccess file which provides nicer way to url rewrite feature in Apache web server.

Also dynamic languages like PHP, Ruby, Python prvoides such a nice facility so that dynamism is achieved using their core feature.