5 MVC Frameworks For PHP Developers in 2016

PHP is one of the most popular open source server side scripting language in the world. The reason behind its popularity is, it is very easy to learn and understand. PHP is more flexible when it comes to writing code because of its features like, weakly typed variables, OOP is not a mandatory.

However, there is a bad side of all this flexibility, it is very easy to write bad code. And the solution to this problem is FRAMEWORK.

There are many benefits of using a framework;

  1. Organization of code is very easy as most the frameworks follow MVC pattern
  2. Most of the frameworks have active community support.
  3. Faster development
  4. Form validation
  5. Pretty URLS

In this post, we are going to go over 5 most popular frameworks in PHP community.

Laravel

Created by Taylor Otwell, Laravel is one the most popular framework. It is one of the most forked project framework on github with very active community support. Laravel’s features include RESTful routing, Blade templating engine, Eloquent ORM, Socialite, Middlewares etc.

laravel

laravel

CodeIgniter

Codeigniter was released in 2006, it is a lightweight PHP framework that comes with unparalled flexibilty and extraordinary documentation. This is the best framework for beginners as it is very easy to understand and learn.

codeignitor

Symfony

Symfony is known as one of the best option for large scale enterprise applications. Licensed under MIT license, it was created by Sensiolabs in 2005. Symfony components are used by many popular open source projects including Drupal, PHPbb and Laravel.

symfony

symfony

CakePHP

CakePHP was initially in 2005 and is one of the most popular framework till date. some of the features of CakePHP are code generation and scaffolding, CSRF protection, form validations, built-in ORM, Helpers, Behaviours, and Plug-ins etc.

cakephp

cakephp

Yii

Yii is a open source PHP framework that follows the MVC pattern. It comes bundled with tons of features that include, a powerful caching system, form validations, integrated with jQuery, and AJAX-enabled features etc.

yii

Time Ago Function in PHP

Here’s a lightweight PHP function which accepts a date as a parameter and converts it into “sometime age” output.

Read Also: How to enable mod_rewrite in Ubuntu

function time_ago($date) {
    if (empty($date)) {
        return "Please enter a date!";
    }
    $duration_in = [
        "second", 
        "minute", 
        "hour", 
        "day", 
        "week", 
        "month", 
        "year", 
        "decade"
    ];
    $len = [
        "60", 
        "60", 
        "24", 
        "7", 
        "4.35", 
        "12", 
        "10"
    ];
    $now = time();
    $unixDate = strtotime($date);

    if (empty($unixDate)) {
        return "Please enter a valid date!";
    }

    if ($now > $unixDate) {
        $diff = $now - $unixDate;
        $tense = "ago";
    } else {
        $diff = $unixDate - $now;
        $tense = "from now";
    }
    for ($j = 0; $diff >= $len[$j] && $j < count($len) - 1; $j++) {
        $difference /= $len[$j];
    }
    $diff = round($diff);
    if ($diff != 1) {
        $duration_in[$j].= "s";
    }
    return "$diff $duration_in[$j] {$tense}";
}

Usage:

echo time_ago($date);

Source: http://stackoverflow.com/a/18891474

How to convert a negative number to a positive number using PHP

PHP comes with a built-in function called abs() [Absolute Value] to convert a negative number to positive number. It accepts one parameter the number itself. The number can be a integer or a float. If the parameter supplied to the function is float it will return float value. Otherwise, it will return an integer.

Changing AngularJS templating brackets

Examples:

<?php
echo abs(-6.2);  // 6.2 (float)
echo abs(19);    // 19 (integer)
echo abs(-2);    // 2 (integer)
?>

 

How to Get Yesterday’s Date Using PHP?

You can use the following code to get yesterday’s date in PHP. To do this, we are using PHP’s built-in date() function.

Quick Tip : How to Get Previous / Next Record in Laravel?

<?php echo date("Y-m-d", time() - (60*60*24) ); ?>

<?php echo date("Y-m-d", strtotime("-1 day"));  ?>

<?php echo date("Y-m-d", strtotime("yesterday")); ?>

<?php echo date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")-1,date("Y"))); ?>

For more information : http://php.net/manual/en/function.date.php

How To Get An App ID and Secret Key From Facebook

In this tutorial, we are going to create a new Facebook App in order to generate a Facebook App ID and Secret Key. To create a new Facebook App follow the steps given below,

Quick Tip: Changing the Laravel Redirect Location After Login

1. Register A New App:

To start the process Click Here. This will take you to the Facebook’s Developer page. You will be asked to login with your Facebook Account.

Once you’re logged in, you will see a webpage similar to one below,

create-facebook-app-step-01

To create a new app click on Add a New App button.

2. Select Platform For Your App:

After you click the Add a New App button, you will be asked to select a new platform for your app. You need to select website.

create-facebook-app-step-02

 

 

3. Create a New App ID:

After select the app platform, you will see the following page. Click on Skip and Create App ID link.

create-facebook-app-step-03

 

 

Then it will ask you for Display Name and select a category ( Recommended : “Apps for Pages” ) .

create-facebook-app-step-04

 

After you’re done with the requested fields, click on Create App ID button. This will task you to the app dashboard.

4. Retrieve the App ID and Secret Key:

create-facebook-app-step-05

 

On the dashboard, you are able to access the app ID and app secret, click on the show button to display app secret.

How to Install Composer on Windows with XAMPP

In this tutorial, we are going to install Composer on Windows 7 computer with Xampp.

Composer is a dependency Management software built specially for PHP. It allows you to install PHP libraries from Packagist, which is its main package repository containing all the available packages.

Useful Read: How to remove last comma from string using php?

To install Composer on your Windows computer follow the steps given below;

1. Download the Composer

Visit the official Composer website and click on the download link.

Installing Composer on Windows Step 01

Installing Composer on Windows Step 01

This will take you to the composer download page. On this page look for Windows Installer section. Here you will see a link to download Composer-Setup.exe file. Click on that link to download the process.

Download Composer For Windows

Download Composer For Windows

2. Start the Installation Process

After successfully completion of the download process, double click the downloaded file to start the installation process. You will see a window similar to one shown in the following image.

 

Installing Composer on Windows Step 02

Installing Composer on Windows Step 02

Click on the next button to proceed the installation process. In the next step, it will ask for your permission to install the shell menus. Select the Do not install Shell Menus and hit the next button.

Installing Composer on Windows Step 03

Installing Composer on Windows Step 03

In the next step, you need to specify the path to your php.exe file located inside xamp directory. Usually, path to php.exe is C:\xampp\php\php.exe. But, it may vary depending on in which drive your XAMPP installation is located e.g. in my case it is D:\xampp\php\php.exe. In most of the situations it will detect the path to php.exe file but if it doesn’t you have to manually locate the file using browse option. After, specify the path click the next button.

Installing Composer on Windows Step 04

Installing Composer on Windows Step 04

This will take you to the pre-installation(Ready to install) window. Hit the install button to start the actual installation.

Installing Composer on Windows Step 05

Installing Composer on Windows Step 05

After successful installation you will see a screen similar to one below. Click finish to complete the installation process.

Installing Composer on Windows Step 06

Installing Composer on Windows Step 06

To test if the composer is working properly, press Windows + R to start the run program and inside that program type cmd and click ok to start the command prompt.

Start Composer 01

Start Composer 01

This will open the command prompt window,

Start Composer 02

Start Composer 02

In the command prompt, type composer and hit enter. This will display your composer’s version number and various options(command) available with composer. If you are able to see these options then you have installed composer successfully.

Start Composer 03

Start Composer 03

In case, you have any comments or queries feel free to comment.

How To Remove Last Comma From String Using PHP?

Generally, developers use loops to create a comma separated lists in string format. But, after the loop you end with a trailing comma at the end of the string.

Useful Read : Export the MySQL database table as CSV format using PHP

To remove this comma we are going to use PHP’s built rtrim function. rtrim function is used to strip off whitespaces and predefined characters from right side of the string. It accepts two parameters,

  1. String – String we want to trim (Required)
  2. Character – Characters which we want to strip (Optional)

Here’s an example code:

$string = "a, b, c, d,";
$filtered = rtrim($string, ',');
echo $filtered;

As you can, we are passing a variable with a comma separated list and a comma(,) as second parameter. The output of above code is as follows.

a, b, c, d

 

How to Get Facebook Like Count With PHP? (Updated)

This is an updated and working code snippet which can be used to retrieve Facebook like count of a specific page. To use this code snippet you’ll need a Facebook App ID and Secret Key.

In order to get an App ID and Secret Key, you have to create a new Facebook application.

Quick Tip : Extract the archive contents with PHP

Here’s a Quick Tutorial Which help you with that,

http://coffeecupweb.com/how-to-get-an-app-id-and-secret-key-from-facebook/

To get Facebook page like count use the following code.

//Get Facebook Likes Count of a page
function fbLikeCount($id,$appid,$appsecret){
	//Construct a Facebook URL
	$json_url ='https://graph.facebook.com/'.$id.'?access_token='.$appid.'|'.$appsecret;
	$json = file_get_contents($json_url);
	$json_output = json_decode($json);
 
	//Extract the likes count from the JSON object
	if($json_output->likes){
		return $likes = $json_output->likes;
	}else{
		return 0;
	}
}
//This Will return like count of CoffeeCupWeb Facebook page
echo fbLikeCount('coffeecupweb','__my_app_id__','__my_secret_key__');

To extract the like count you need to page id or name, app id and secret key.

Facebook Graph API 2.4 Update:

The above code is working for apps below 2.4 version, but for apps above 2.4 you need to use following code.

//Get Facebook Likes Count of a page
function fbLikeCount($id,$appid,$appsecret){
	//Construct a Facebook URL
	$json_url ='https://graph.facebook.com/'.$id.'?access_token='.$appid.'|'.$appsecret.'&fields=likes';
	$json = file_get_contents($json_url);
	$json_output = json_decode($json);

	//Extract the likes count from the JSON object
	if($json_output->likes){
		return $likes = $json_output->likes;
	}else{
		return 0;
	}
}
//This Will return like count of CoffeeCupWeb Facebook page
echo fbLikeCount('coffeecupweb','__my_app_id__','__my_app_secret__');

As you can see, with Graph API 2.4 we need to pass name of fields we need to access. Apart from likes you can also access,

  1. id
  2. about
  3. birthday
  4. category
  5. likes
  6. link
  7. name
  8. talking_about_count
  9. username
  10. website
  11. were_here_count