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.

3 Replies to “Laravel Routes & Controllers”

Leave a Reply

Your email address will not be published. Required fields are marked *