Skip to content

Instantly share code, notes, and snippets.

@rawaludin
Created January 16, 2014 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rawaludin/8450747 to your computer and use it in GitHub Desktop.
Save rawaludin/8450747 to your computer and use it in GitHub Desktop.
How to create custome route in service provider in Laravel 4

Kalau mau naruh route di tempat terpisah pakai service provider gan. http://laravel.com/docs/ioc#service-providers

Contohnya, ane kalau pakai class bikinan sendiri di buat di dalam namespace 'Morphaworks'. Struktur project ane kayak gini..

app
- commands
- config
- ....
- Morphaworks
- - RouteServiceProvider.php
- start

Isi dari RouteServiceProvider.php

<?php namespace Morphaworks;
use Illuminate\Support\ServiceProvider;

class RouteServiceProvider extends ServiceProvider {
    public function register()
    {
        $this->app['router']->get('urlGue', function()
        {
            return 'Halo, halo! Ini dari url dari ';
        });
    }
}

Kalau mau nambah, tambah di bagian $this->app['router']->get() atau $this->app['router']->post()

Tambah service provider ini di app/config/app.php

    'providers' => array(
        ....
        'Morphaworks\RouteServiceProvider'
    )

Di composer.json dibagian autoload isi kayak gini..

"autoload": { 
  ...
  "psr-0" : {"Morphaworks":"app"}
}

Terakhir, reload autoload composer..

$ composer dump-autoload

Cek url yang baru..

$ php artisan routes

Test deh, url yang baru..

http://localhost:8000/urlGue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment