PHP

Laravel 5

Introduction

Laravel is a free and open-source PHP web framework like CodeIgniter and Zend. It is built on the Model-View-Controller (MVC) architectural pattern. It has very good documentation and training material. The community is also significant enough. I find it to be much easier than Zend and about as easy to use as CodeIgniter. Better training material and documentation makes Laravel easier for beginner than CodeIgniter.

MongoDB

MongoDB is document based NoSQL database. This means that:

  • it has no schema
  • it has no relations
  • it is agile and scaleable
  • stores information in nested documents of two-dimensional key/value pairs.

What are the benefits of using MongoDB?

  • you do not have to pre-define your schema or data model
  • works nicely with JavaScript
  • ideal for client-side applications

Installing MongoDB

Installing MongoDB on Mac with Homebrew

Creating a view that lists nodes tagged by a taxonomy term

Following is what we will be creating. A view that will create a page with a list of nodes (titles + teasers) and a block (with node titles only) of all nodes tagged by a taxonomy term.

Assumption: - You have multiple nodes tagged with term "PHP" - The nodes are of type Article

Go to Structure > Views > Add a new view and fill in the form as follows:

View name: PHP Show Content of type Article tagged with PHP sorted by Newest first check Create a page check Create a block Save & Exit

Then go to Structure > Blocks to enable the block.

Type casting in PHP

PHP does not support explicit type definition in variable declaration. For example, you CANNOT declare a variable as follows:

int $count = 10;

In PHP, a variable's type is determined by the context in which the variable is used.

Where are PHP error logs

PHP errors are by default set to go to the apache error log. On Ubuntu, the error log is located at /var/log/apache2/error.log. If you are a PHP developer, it is good idea to open a small terminal window and type the following:

tail -f /var/log/apache2/error.log

This would essentially give you a live view of the error log. As you generate errors, you would see the error messages on the terminal.

PHP usort explained

The usort function sorts an array by values where the values are sorted by a user-defined function. usort has the following syntax

bool usort ( array &$array , callback $cmp_function )

The comparison function takes two arguments and it must return integer values, either 0, positive integer, or negative integer.

PHP Warning: strtotime(): It is not safe to rely on the system's timezone settings.

If a default timezone is not set in your php.ini, then you get the following warning:

"Warning: strtotime(): It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Denver' for 'CDT/-6.0/DST'"

To get rid of this warning, open you php.ini file and set a value for date.timezone

Converting string to date in PHP

Converting strings to dates in PHP requires the use of two functions. strtotime function converts the string to an epoch timestamp. date function converts the timestamp to the desirable date format.

<?php
   $string = "Nov 11, 2011";
   $timestamp = strtotime($string);
   $date = date("Y-m-d", $timestamp);
   print $timestamp . "<br>" . $date;
?>

output

Installing mahara on Ubuntu

You can now install Mahara using the Synaptic Package Manager. But before you install mahara, you should create a database for mahara in MySQL or PostgreSQL. You will be asked for the database connectivity information during installation.

$ sudo apt-get install mahara

Once installation is complete, go to the mahara installation directory

$ cd /usr/share/mahara

and backup the config file

How can I change the filesize limit for imported files in PhpMyAdmin

The filesize limit is not defined in PhpMyAdmin. The limit is taken from php.ini file. To change this limit, you needed to edit your php.ini file:

$ locate php.ini
$ cd /etc/php5/apache2/
$ sudo cp php.ini php.ini.original  
$ sudo vi php.ini

Change the following two variables in the php.ini file:

upload_max_filesize = 50M
post_max_size = 200M

This would allow file uploads of 50 megabytes. You need to restart apache before these changes can take place. On Ubuntu, the command is

Pages