Laravel Quickies

Laravel Quickies is a quick tutorial I developed to help you remember how to do the major coding steps for the Laravel MVC PHP framework. If you don’t have an idea of how to do the things in this tutorial yet, you should probably read the more detailed tutorials I’ve written on each of the subjects below. This is meant just as a quick reference guide for those that already know the basics but need a quick refresher course in the high level steps to do things such as create a route, model, view or migration for example.

Laravel Quick Start Examples

Routes:

Enter the following code into routes.php to create a simple route:

Route::get(‘test’, function(){

return(‘<h1>Laravel Route Example</h1>Ian L. of Jafty.com created this simple Laravel route test.’);

});

visit the full Laravel routes tutorial here: http://jafty.com/blog/laravel-routes/

Controllers:

Add the following in a new file within the app/controllers directory to create a new controller:

class yourClass extends BaseController {

public function yourFunction() {

return “<h1>This is what gets output to the web page when the test route is visited by a user in this basic Controller Example</>”;

}

The above controller would need the following route set up in routes.php to make it work:

Route::get(‘test’, ‘yourClass@yourFunction’);

Finally, after you create a new controller, you always need to flush the autoload contents before the new controller will work, so do so by opening your command prompt that you use composer from and type:

composer dump-autoload

More Laravel quickies coming soon! For now, you can view all of my Laravel tutorials and posts by visiting the Laravel Category of my blog HERE!

Laravel Routes & Controllers

One of the first things I learned when discovering the Laravel PHP framework, was how to use routes. Routes are a simple method of using sub-directories in a URL to show different content. For example you can set a route to “about” and code that route to show your about page content. Let’s expand on that idea with a code example to help you get it:

Go to your project directory in Laravel and locate the app directory and the file name routes.php. This is where you can add your own routes. Lets start with simple route by adding the following code to routes.php at the end of the file:

Route::get(‘about’, function(){

return(‘<h1>About Us</h1>Jafty.com is a fully functional web development company offering almost any service to do with web development, including, but by no means limited to, PHP, HTML5, CSS3, JavaScript, Photoshop, Logo Design, Jquery, Server Installation, Site Migration, WordPress, Joomla, Linux servers, Windows servers, etc. Just ask, I pretty much do it all. Yes, Jafty.com is basically, jut me, Ian L. I do hire additional help as needed ,but I personally manage and oversee every project that comes through my company and make sure that it meets or exceeds my rigid quality control standards.’);

});

You can copy and paste the above green text into your app/routes.php file at the end and it will produce the following when you go to (in my case)localhost/laravel/project1/public/about. Here is a screenshot:

Snap 2013-09-19 at 12.13.15Now you may not wish to put all that code inside your routes.php file, especially in times where you need a lot more code for a page. You can use controller classes with your routes to effectively put most of the code in another file which I will explain next.

Here is an example of a basic controller class:

class UserController extends BaseController {

    public function showWelcome()
    {
        return ‘Hello controller world!’;
    }

}

to make that class work, you would put the above code in a file named “UserController.php” inside of the app/controllers folder of your Laravel project directory and then add the following to your app/routes.php file at the end:

Route::get(‘usercontrol’, ‘UserController@showWelcome’);

Finally, after you create a new controller, you always need to flush the autoload contents before the new controller will work, so do so by opening your command prompt that you use composer from, change the working directory to that of your application and type:

composer dump-autoload

I’ve found you don’t need to do the above command if you only create a route, but if you make a controller, do it! Also if you have issues running the above command from localhost, make sure you have the correct path and command prompt. If you installed Composer through Windows, open a Windows command prompt. If you installed it in Xampp, open a Xampp command prompt etc.

That’s all there is to learning how to use basic routes and basic controllers. Of course there are more complex solutions than this, but this will get you started off on the right foot at least. To learn more, CLICK HERE FOR MORE LARAVEL TUTORIALS. I have written a boat load of Laravel tutorials on this blog and am adding more daily as of September of 2013, so check back if it is still 2013 when you read this or the start of 2014 even.