menu

Laravel - Grouping Route 1


Melanjutkan tutorial yang kemarin, pada tutorial kali ini saya akan memberikan tutorial tentang cara membuat group route pada Laravel.

Membuat group merupakan suatu trik untuk mengelompokan route menjadi beberapa kelompok sehingga memudahkan seorang developer untuk mentrack controller suatu project.

Selain memudahkan developer grouping route ini juga membuat codingan menjadi lebih rapi dan elegan.

Oke langsung saja kita mulai tutorialnya,.,.,

Pertama edit routes.php pada path protected/app/routes.php menjadi seperti ini
<?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('/', 'HomeController@showWelcome');

//pahamilah group route dibawah
Route::group(array('prefix' => 'crud'), function(){
 Route::get('users', 'Controller\Crud\ReadController@readUsers');

 Route::get('create', 'Controller\Crud\CreateController@createUser');
 Route::post('create', 'Controller\Crud\CreateController@createpostUser');

 Route::get('update/{id}', 'Controller\Crud\UpdateController@updateUser');
 Route::post('update/{id}', 'Controller\Crud\UpdateController@updatepostUser');

 Route::get('delete/{id}', 'Controller\Crud\DeleteController@deleteUser');
});

Selanjutnya edit readUser.blade.php, createUser.blade.php, updateUser.blade.php  pada path protected/app/view/web/, masing-masing menjadi seperti ini.

readUser.blade.php
.
.
.
          <td>
           <a href="{{ URL::to('crud/update/'.$users->id) }}">Edit<br>
           <a href="{{ URL::to('crud/delete/'.$users->id) }}">Delete
          </td>
       </tr>
     @endforeach
   </table>
   <br>
   <button onclick="location.href='{{ URL::to('crud/create') }}';">Create</button>
 </div>
@stop

createUser.blade.php
.
.
.
   <tr>
    <td colspan="3">
     <br>
     <input type="submit" class="btn" value="Create">
    </td>
   </tr>
  </table>
  </form>
 </div>
@stop

updateUser.blade.php
@extends('layouts/web/master')
@section('content')
 <?php $title = "Update User" ?>
 <div class="isi">
  <form method="post" action="">
  <table>
   <tr>
    <td>Name</td>
    <td nowrap>:</td>
    <td><input type="text" name="name" value="{{ $data_user['name'] }}"><span style="color:red"><?php echo $errors->first('name') ?></td>
   </tr>
   <tr>
    <td>Username</td>
    <td nowrap>:</td>
    <td><input type="text" name="username" value="{{ $data_user['username'] }}"><span style="color:red"><?php echo $errors->first('username') ?></td>
   </tr>
   <tr>
    <td>Email</td>
    <td nowrap>:</td>
    <td><input type="text" name="email" value="{{ $data_user['email'] }}"><span style="color:red"><?php echo $errors->first('email') ?></span></td>
   </tr>
   <tr>
    <td colspan="3">
     <br>
     <input type="submit" class="btn" value="Save">
    </td>
   </tr>
  </table>
  </form>
 </div>
@stop

Kemudian edit header.blade.php pada path protected/app/view/layouts/web menjadi seperti ini.
<div class="header">
 <div class="banner">
  <div class="welcome">Welcome</div>
  {{-- code dibawah ini menggunakan constant variabel --}}
  <div class="gambar"><a href="#"><img src="{{ IMAGE_HEADER }}" style="width:"100px" height="129px";"></a></div>
 </div>
 <hr>
 <div class="menu">
  <div class="kiri">
   <li>
    <div class="dropdown">
      <button class="button">Menu(css)</button>
      <div class="dropdown-content">
        <a href="{{ URL::to('crud/users')}}">User</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
      </div>
    </div>
   </li>
   <li>
     <div class="dropdown-script">
      <button onclick="myFunction()" class="dropbtn">Menu(css)</button>
      <div id="myDropdown" class="dropdown-content-script">
        <a href="{{ URL::to('crud/users')}}">User</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
      </div>
     </div>
   </li>
  </div>
  <div class="kanan">
   <li onclick="location.href='{{ URL::to('/') }}';">Login</li>
   <li onclick="location.href='{{ URL::to('/') }}';">Cari</li>
  </div>
 </div>
</div>

Dan yang terakhir edit controller CreateController.php, UpdateController.php, dan DeleteController.php masing-masing seperti ini.

CreateController.php
.
.
.
     if ($validator->passes()) {
         $user           = new User;
         $user->name     = Input::get('name');
         $user->username = Input::get('username');
         $user->email    = Input::get('email');
         $user->password = Hash::make(Input::get('password'));
         $user->save();

         return Redirect::to('crud/users');
     }
     else {
      return Redirect::to('crud/create')
       ->withErrors($validator)
       ->withInput();
     }
 }
}

UpdateController.php
.
.
.
     if ($validator->passes()) {
         $user           = User::find($id);
         $user->name     = Input::get('name');
         $user->username = Input::get('username');
         $user->email    = Input::get('email');
         $user->save();

         return Redirect::to('crud/users');
     }
     else {
      return Redirect::to('crud/update/'.$id)
       ->withErrors($validator)
       ->withInput();
     }
 }
}

DeleteController.php
<?php namespace Controller\Crud;

use Model\User;
use \Redirect;

class DeleteController extends \BaseController 
{
 public function deleteUser($id)
 {
  $user = User::where('id', $id)->first();
  $user->delete();
  return Redirect::to('crud/users');
 }
}

Silahkan pahamilah sendiri apa saja yang telah kita edit dari tutorial sebelumnya,..

===DONE!===