PSR-4 and Laravel

If you didn’t already know, psr-* is a php standard for auto-loading classes, it stands for PHP Specification Request, psr-4 is the latest version.

PSR-4 offers great flexibility, you can define namespaces with any location, for example we might use Acme namespace that looks for classes in a folder called app/BusinessLogic/ and later on, if you want to change the location you just change the location where you declare the namspace(composer.json) without touching any of you files.

PSR-4 namespace(s) can be defined within composer.json in autoload section:


"autoload": {
		"classmap": [
			"app/database/migrations",
			"app/database/seeds"
		],

        "psr-4": {
            "Acme\\": "app/BusinessLogic/",

        }
},

Note: “\\” is required at the end.

Let’s say we have class A inside BusinessLogic/, we will define Acme namespace and access the class like this:


new Acme\A();
Advertisements

Where do I put events? or bindings?

Laravel offers tons of features but no strict conventions, this is something I like a lot but can be discouraging for beginners because they don’t really know where to put their code (besides the mvc paradigm)

Events and Binds need to be injected in Laravel request cycle before routing, a good way of structuring them is to create 2 files in app/ :  binds.php and events.php which you include in app/start/global.php, at the end of the file:

require app_path().'/binds.php';

require app_path().'/events.php';

There really isn’t the “correct way”, it depends on you needs and your style. The example above is just one way of doing it.

Getter & Setter Methods in Eloquent

As I mentioned before, Laravel offers this 2 methods to handle how information is displayed or changed before insert.

The rules are simple: you declare them by appending “set_”  or “get_” to the intended attribute’s name.

Setter example and usage

To format a date:

public function get_date_start()
{

   return date('M d, Y', $this->get_attribute('date_start')));

}

Now everytime you use $object->date_start, it will be formatted accordingly.

Getter example and usage

To encrypt a password before inserting in the database:

public function set_password($password) 
{ 
    $this->set_attribute('password', Hash::make($password)); 
}

How to user MySQL functions with eloquent and fluent

Both Eloquent ORM and Fluent query builder are wonderful tools, but, sometimes you want more, you may for example want to concatenate 2 fields (for presentation purposes Eloquent provides Getter & Setter Methods, but that’s another story for another time).

This:


 return self::get( array( 'id', 'CONCAT(first_name, " ", last_name)' ) );

Won’t work, to insert MySQL functions you have to use DB::raw() method:


 return self::get( array( 'id', DB::raw('CONCAT(first_name, " ", last_name)') ) );

PHP get last month in March bug

This post is not laravel related, but is a big bug in PHP regarding how it handles months, especially February.

If you need  the last month, today(March 29), with one of the following ways



 date('Y-m-d', strtotime('-1 Month'));

 date('Y-m-d', mktime(0,0,0,date("m")-1,date("d"),date("Y")) );

 $date = new DateTime();
 $date->modify("-1 Month");
 $date->format("Y-m-d");


They all return “2013-03-01”, which can screw up your applications big time.

The solution is:


 date('Y-m-d', strtotime('first day of last month'));

Which returns “2013-02-01”;