How to create routes in Laravel 4.2 - BOOKC -->

How to create routes in Laravel 4.2

Wednesday, April 20, 2016


Before i show you how to create controller in laravel 4.2, let me explain little bit about routes in laravel 4.2, let's create a simple routes.

In laravel 4.2, the routes is located at app/routes.php, if you want to create new routes place it inside that file, okay now let's take a look at routes.php.

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

Route::get('/', function()
{
return View::make('hello');
});

Let's add new routes for url /testing, like this:

Route::get('/testing/', function()
{
return 'visit laravelhowto.blogspot.com';
});

Go ahead open url http:/localhost/testing or http://localhost:8000/testing and see what's happen. Note that it's a bad practice if you put application logic inside routes.

Never put application logic inside routes, instead doing that, you can create controller and put application logic in there and then call the controller using routes.

Here's an example of routes that calling controller with specific method, since you don't have the controller yet you will get error message if you try to open it, this is just an example.

Route::post('THE_URL', 'THE_CONTROLLER_NAME@THE_METHOD_NAME');

Route::post('create-user', 'UserController@postCreateUser');
Route::get('list-user', 'UserController@getListUser');
Route::post('update-user', 'UserController@postUpdateUser');
Route::post('delete-user', 'UserController@postDeleteUser');

Next article i will show you how to create a controller, see ya !