Laravel 4 Base Controller for AngularJS

I’ve recently been doing a lot of work with Laravel 4 and AngularJS and it’s been a great experience so far. The resourceful controllers provided by Laravel integrate really nicely with the HTTP “services” available in AngularJS.

Laravel 4 has a ton of new stuff and among it all I’ve noticed how useful the Input::only() (Laravel Requests Class) function is inside of my controllers. It allows you to grab only a portion of the request data that reaches the controller and is a clean elegant way to pass data along for processing.

When I first started using AngularJS I quickly realized that all of the requests to my existing controllers were sending JSON payloads that were treated a little bit differently than the standard HTTP request data. After some digging around in the Illuminate\HTTP\Request class I found that it was rather trivial to enable the same helper methods for JSON request data.

Below is the code I used in my BaseController and then a sample of an “actual” controller where the parent constructor is invoked.

 

Nice, clean and simple; writing code with Laravel truly is a pleasure!

If you’re interested in trying out Laravel 4 I have a quick and easy vagrant setup available on GitHub.

This entry was posted in AngularJS, Laravel, PHP. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">