Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

SitePoint PHP Blog:
Ardent: Laravel Models on Steroids
Sep 19, 2014 @ 16:54:34

In Ardent, an enhancement to the model system in the Laravel framework that allows for easy configuration of validation rules.

One of the (few) things I don’t like about Laravel is that you cannot move the validation code from your controller to your models easily. When I write software I like to apply the “fat models, skinny controllers” principle. So, for me, writing the validation code in the controller is not a good thing. To solve this, I’d like to introduce Ardent, a great package for Laravel 4. To be more precise, Ardent presents itself as “Self-validating smart models for Laravel Framework 4’s Eloquent ORM.” In other words: exactly what we need!

He introduces the library as a part of a test application, a To-Do list that includes user and task handling. He starts with the creation of the base Laravel migrations to build the tables and the code for the two necessary models. He then shows how to install Ardent and put it to use in the controller code, adding validation rules and messages for each property on failure. He also shows how to use the model auto-hydration and hooks to make working with the models even easier. He finishes off the post showing how to set up relations "the Ardent way" using a slightly different format as the usual Laravel handling.

tagged: laravel model ardent library tutorial introduction

Link: http://www.sitepoint.com/ardent-laravel-models-steroids/

NetTuts.com:
Testing Like a Boss in Laravel: Models
Feb 19, 2013 @ 19:42:04

NetTuts.com has a new tutorial in their series covering the use of the Laravel 4 framework. In this new post, though, they show how to ensure quality in your application by unit testing your models (the "M" in "MVC").

If you’re hoping to learn why tests are beneficial, this is not the article for you. Over the course of this tutorial, I will assume that you already understand the advantages, and are hoping to learn how best to write and organize your tests in Laravel 4. Version 4 of Laravel offers serious improvements in relation to testing, when compared to its previous release. This is the first article of a series that will cover how to write tests for Laravel 4 applications. We’ll begin the series by discussing model testing.

They show how to set up the environment - making it use an in-memory SQLite database, call the migration to initialize it and run a "createApplication" to bootstrap the app. They then include an example if tests written against a "Post" model that extends the Ardent class. It also uses the FactoryMuff package to create objects. Some example tests are included, checking things like object properties, "posted at" date formats and the "slug" of a page (in a Page model and its tests).

tagged: unittest laravel4 application tutorial ardent factorymuff

Link:


Trending Topics: