Getting Started With Laravel 4, Chapter 7: Architecting Ambitious Applications
Moving from simple routing to powerful controllers
You can take the business logic out of your routes, and place them in controllers. Create your route, and place the following in it: array("uses" => controller@function).
Favoring explicit routing
You can use implicit routing in Laravel, much like was done in both Codeingniter and early versions of Ruby on Rails. Explicit routes are better. You can define the name and expected pattern of each argument.
Straightforward REST routing
Instead of specifying all the routes, you can just create one route, and a controller with all the appropriate action verbs.
Supercharging your models
We used simple models earlier, but Eloquent allows for more complex queries that we will examine in the next few sections.
Simple performance tricks
ORM's are great for simplifying database work. But they can cause issues if you don't know what is going on behind the scenes.
Eager loading records
When you've got models with foreign keys, you can preload the extra data. In fact, this is recommended, as it will significantly reduce the number of queries needed to run.
Selecting what you need
Instead of "selecting field from table", you can do Table::get(array("field")). This helps in reducing the amount of data needed to load.
Profiling your queries
There are several different profiles mentioned in the book, that you can download and install in Laravel. These will help you find bottlenecks in your code.
Foolproof models with soft deletes
You can create a model where data is not actually deleted by using $softDelete = true; and adding a column deleted_at as a DATETIME field added to your model. Eloquent just makes this work. No extra work is needed on your part. You can retrieve the deleted records with onlyTrashed()->get(); To permantently delete the record/model, just use forceDelete();
More control with SQL
Eloquent includes a query builder that allows more control, but does not expose your to SQL injection vulnerabilities.
Listening for model events
You can run arbitrary code whenever a model is changed.
The handy paginator
Laravel has a handy paginator to return only a limited set of records. It even provides the blade template to move forward and backward through your record set.
Environment configuration made easy
$environments is an array that will easily allow you to set settings for different hosts, based on name or IP address.
Enviroments and Artisan
If you set an environment variable, you will need to pass that to Artisan with the --env flag. Or you could add your local machine to the environment array.
Adding your own configuration settings
Configuration settings are all just associative arrays stored in files. You can bring them in with Config::get($setting, $default).
Playing nice with the frontend
Laravel automatically serializes arrays and Eloquent collections to JSON. It will also serialize paginated results.
Comments
Post a Comment