Nasrul Hazim Bin Mohamad

Laravel: Managing Routes



Recently I’ve meet some of my friends, and clients working with Laravel projects. It’s seems that, Route, managing routes is quiet painful when your application become bigger and you have to handle a lots of routes.

So, what’s come out of my mind was, how to manage Laravel’s routes more efficient?

I’ve come up with few possible options:

  1. Define more map methods in RouteServiceProvider, and call them – but this will make the RouteServiceProvider full with map methods, which I don’t prefer it that way – to many mapping routes method.
  2. Then I’ve come up with ideas having route trait, then you just need to attach it to any classes which you prefer, then call it any RouteServiceProvider or in routes/web.php. But having trait just to manage the routes, it’s like overkill (up till now), so I skip creating the trait.
  3. Then I’ve come up with having another route folder, purposely to serve the routes – I’ve named it Routes, in app directory. In the Routes directory, you create a class,  and have a method routes. In that method you just need to define your route – but of course, you need to import Route facade. Below is an example of implementation.

Now you should have much cleaner codes in your routes/web.php. You just need to include the route namespace, and call the className::routes() to load the routes.

That will be much cleaner routes. You may want to structure your app/Routes directory to support route’s version, or prefix, etc.

Hopes the idea will help out developers to easily manage their projects.

Hope the approach won’t break the incoming Laravel releases! ^_^

I think that’s what I want to share for now. Thanks for reading!

Training: Web Mobile Application Development (Basic / Intermediate) – INTIM – August 2015


Training: Web Mobile Application Development (Basic / Intermediate)

Location: INTAN Kampus Wilayah Timur (INTIM), Kemaman, Terengganu

Date: 17th – 20th August 2015


Training Materials:

  1. Web Mobile Application Starter Kit –
  2. Basic Android Configuration –
  3. API Template –

Important Articles:

  1. HTTP Methods for Restful Services –
  2. Android Configuration –
  3. Chrome Device Mode –


Quick Development: Slim Framework + Illuminate(Laravel)


Create a project folder and add a composer.json file in it and paste the following codes, and run composer install. You should have Slim Framework, Twig, Illuminate(Laravel) in the project folder.

    "name": "Quick Dev!",
    "require": {
        "php": ">=5.3.0",
        "slim/slim": "2.*",
        "slim/extras": "2.*",
        "twig/twig": "1.*",
        "illuminate/database": "*"
    "autoload": {
        "classmap": [

Add a index.php file in project folder and copy paste the following:


require 'vendor/autoload.php';

// Database information
$settings = array(
    'driver' => 'mysql',
    'host' => '',
    'database' => 'dbname',
    'username' => 'username',
    'password' => 'password',
    'collation' => 'utf8_general_ci',
    'prefix' => '',
    'charset' => 'utf8'

// Bootstrap Eloquent ORM
$container = new IlluminateContainerContainer;
$connFactory = new IlluminateDatabaseConnectorsConnectionFactory($container);
$conn = $connFactory->make($settings);
$resolver = new IlluminateDatabaseConnectionResolver();
$resolver->addConnection('default', $conn);

$app = new SlimSlim();

$app->get('/', function () {
    echo "<h1>Quick Dev!!</h1>";
    $articles = Article::all();
    echo $articles->toJson();


Create a folder named models in the project folder and add the following into a file named Article.php:


class Article extends IlluminateDatabaseEloquentModel


Don’t forget our .htaccess file in a project folder. 🙂

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Now you can navigate to your system, for example: http://localhost/quick-dev

p/s: You can use Twig later on. 🙂