2016-09-17 79 views
1

我是laravel的新手。我想知道如何在最佳實踐中編寫Laravel 5路線。Laravel 5路線最佳實踐?

這是我的路線到目前爲止。

<?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 controller to call when that URI is requested. 
| 
*/ 

/* start contoh acl simple 

Route::get('owner', ['middleware' => 'role:owner', function() { 
    return 'cie owner masuk :p'; 
}]); 

Route::get('manager', ['middleware' => 'role:manager', function() { 
    return 'halo pak manager'; 
}]); 

Route::get('denied/{role}', function ($role) { 
    if($role){ 
    return 'hayo ngapain akses halaman '.$role.', emang kamu siapa? kan cuma client :p cie dah paham middleware nieh'; 
    } 
    return 'boleh lah akses sini'; 
}); 

end contoh acl simple */ 

// use Illuminate\Support\Facades\App; 
// use LaravelPusher; 
// Route::auth(); 
Route::get('/jembatan', function(){ 
    // $message = 'mantap coyyy'; 
    // $pusherx = App::make('pusher'); 
    // LaravelPusher::trigger('my-channel', 'my-event', ['message' => $message]); 
    $messages = [ 
    "id" => 5, 
    "jenis" => 'afdsa', 
    "no_kendaraan" => 'L 1334 H', 
    "status" => 'Menunggu', 
    ]; 
    LaravelPusher::trigger('antrian', 'newAntrian', $messages); 
    return 'success'; 
}); 

Route::get('/', 'Auth\[email protected]'); 
Route::post('/', 'Auth\[email protected]'); 
///* 
Route::get('/register', 'Auth\[email protected]'); 
Route::post('/register', 'Auth\[email protected]'); 

Route::get('/password/reset/{token?}', 'Auth\[email protected]'); 
Route::post('/password/reset', 'Auth\[email protected]'); 

Route::post('/password/email', 'Auth\[email protected]'); 

Route::get('/logout', 'Auth\[email protected]'); 

//*/ 

Route::get('/home', '[email protected]')->name('home'); 

//route inti 

Route::get('layarAntrian/{id}', '[email protected]')->name('guest.layarAntrian'); 

//owner 
Route::group(['prefix' => 'owner', 'middleware' => ['auth', 'role:owner']], function() { 

    //input showrooms baru >> assign manager (detail n salary) 
    Route::get('showrooms', '[email protected]')->name('owner.showrooms'); 

    Route::get('showrooms/create', '[email protected]')->name('owner.showrooms.create'); 

    Route::post('showrooms/create', '[email protected]')->name('owner.showrooms.store'); 

    Route::get('showrooms/{id}/show', '[email protected]')->name('owner.showrooms.show'); 
    Route::get('showrooms/{id}/delete', '[email protected]')->name('owner.showrooms.destroy'); 

    Route::get('showrooms/{id}/edit', '[email protected]')->name('owner.showrooms.edit'); 

    Route::patch('showrooms/{id}/update', '[email protected]'); 


    //input manager baru 
    // /managers 
    Route::get('managers', '[email protected]')->name('owner.managers.index'); 
    Route::get('managers/{id}/show', '[email protected]')->name('owner.managers.show'); 
    Route::get('managers/create', '[email protected]')->name('owner.managers.create'); 
    /* 
    Route::get('managers/create', function(){ 
    return 'x bro'; 
    }); 
    */ 

    Route::post('managers/create', '[email protected]')->name('owner.managers.store'); 

    Route::get('managers/{id}/delete', '[email protected]')->name('owner.managers.destroy'); 

    Route::get('managers/{id}/edit', '[email protected]')->name('owner.managers.edit'); 
    Route::patch('managers/{id}/update', '[email protected]')->name('owner.managers.update'); 

}); 


//manager 
Route::group(['middleware' => ['auth', 'role:manager']], function() { 

    //laporan keuangan, stock, aset 

    //input accountant, operator, cleaner 
    Route::get('employees', '[email protected]')->name('manager.employees.index'); 
    Route::get('employees/{id}/show', '[email protected]')->name('manager.employees.show'); 

    Route::get('employees/create', '[email protected]')->name('manager.employees.create'); 
    Route::post('employees/store', '[email protected]')->name('manager.employees.store'); 

    Route::get('employees/{id}/edit', '[email protected]')->name('manager.employees.edit'); 
    Route::patch('employees/{id}/update', '[email protected]')->name('manager.employees.update'); 

    Route::get('employees/{id}/delete', '[email protected]')->name('manager.employees.destroy'); 

    //aturan tarif, gaji, insentif 
     // /pricings /pricings/create /pricings/{id} /pricings/{id}/edit 
    Route::get('pricings', '[email protected]')->name('manager.pricings.index'); 
    Route::get('pricings/{id}/show', '[email protected]')->name('manager.pricings.show'); 

    Route::get('pricings/create', '[email protected]')->name('manager.pricings.create'); 
    Route::post('pricings/store', '[email protected]')->name('manager.pricings.store'); 

    Route::get('pricings/{id}/edit', '[email protected]')->name('manager.pricings.edit'); 
    Route::patch('pricings/{id}/update', '[email protected]')->name('manager.pricings.update'); 

    Route::get('pricings/{id}/delete', '[email protected]')->name('manager.pricings.destroy'); 

    // /salaries /salaries/create /salaries/{id} /salaries/{id}/edit 


    // /bonuses /bonuses/create /bonuses/{id} /bonuses/{id}/edit 

    // pindahan dari operator 
    Route::get('materials', '[email protected]')->name('manager.materials.index'); 
    Route::get('materials/{id}/show', '[email protected]')->name('manager.materials.show'); 
    Route::get('materials/create', '[email protected]')->name('manager.materials.create'); 
    Route::post('materials/store', '[email protected]')->name('manager.materials.store'); 
    Route::get('materials/{id}/edit', '[email protected]')->name('manager.materials.edit'); 

    Route::get('materials/{id}/stock', '[email protected]')->name('manager.materials.stock'); 
    Route::post('materials/{id}/update_stock', '[email protected]')->name('manager.materials.updatestock'); 

    Route::patch('materials/{id}/update', '[email protected]')->name('manager.materials.update'); 
    Route::get('materials/{id}/destroy', '[email protected]')->name('manager.materials.destroy'); 

    Route::get('assets', '[email protected]')->name('manager.assets.index'); 
    Route::get('assets/{id}/show', '[email protected]')->name('manager.assets.show'); 
    Route::get('assets/create', '[email protected]')->name('manager.assets.create'); 
    Route::post('assets/store', '[email protected]')->name('manager.assets.store'); 
    Route::get('assets/{id}/edit', '[email protected]')->name('manager.assets.edit'); 
    Route::patch('assets/{id}/update', '[email protected]')->name('manager.assets.update'); 

    Route::get('assets/{id}/stock', '[email protected]')->name('manager.assets.stock'); 
    Route::post('assets/{id}/update_stock', '[email protected]')->name('manager.assets.updatestock'); 

    Route::get('assets/{id}/destroy', '[email protected]')->name('manager.assets.destroy'); 

}); 

//operator 
Route::group(['middleware' => ['auth', 'role:operator']], function() { 

    //tambah pelanggan 
    //probably unused 
    /* 
    Route::get('clients', '[email protected]')->name('operator.clients.index'); 
    Route::get('clients/{id}/edit', '[email protected]')->name('operator.clients.edit'); 
    Route::patch('clients/{id}/update', '[email protected]')->name('operator.clients.update'); 
    Route::get('clients/{id}/delete', '[email protected]')->name('operator.clients.destroy'); 
    */ 

    //real operator 
    // Route::get('clients/create', '[email protected]')->name('operator.clients.create'); 
    Route::get('terimaPelanggan', '[email protected]')->name('operator.terimaPelanggan'); 
    Route::post('clientStore', '[email protected]')->name('operator.clientStore'); 
    Route::get('antrian', '[email protected]')->name('operator.antrian'); 
    Route::get('antrian/{id}/show', '[email protected]')->name('operator.antrian.show'); 
    // Route::get('layarAntrian', '[email protected]')->name('operator.layarAntrian'); 
    Route::get('updateStatus', '[email protected]')->name('operator.updateStatus'); 


}); 


//cashier 
Route::group(['middleware' => ['auth','role:cashier']], function() { 

    //pindahan dari operator 
    Route::get('antrianKasir', '[email protected]')->name('cashier.antrian'); 
    Route::get('antrianKasir/{id}/show', '[email protected]')->name('cashier.antrian.show'); 
    //list transaction in out 
    Route::get('transactions', '[email protected]')->name('cashier.index'); 
    Route::get('transactions/{id}/show', '[email protected]')->name('cashier.show'); 

    //view detail transaction 
    // Route::get('transactions/{id}', '[email protected]')->name('operator.show'); 

    //create transaction form 
    Route::get('transactions/create/{id}', '[email protected]')->name('cashier.create'); 
    Route::get('search/product', '[email protected]')->name('cashier.autocomplete'); 
    Route::get('search/customer', '[email protected]')->name('cashier.autocompleteCustomer'); 
    Route::post('transactions/additem', '[email protected]')->name('cashier.additem'); 
    Route::get('transactions/deleteitem/{id}', '[email protected]')->name('cashier.deleteitem'); 
    Route::post('transactions/clearitems', '[email protected]')->name('cashier.clearitems'); 
    Route::get('transactions/store', '[email protected]')->name('cashier.store'); 


    Route::get('transaction/{id}/export', '[email protected]')->name('cashier.transaction.export'); 

}); 


//client 
Route::group(['prefix' => 'client', 'middleware' => ['auth', 'role:client']], function() { 

    //topup 
    Route::get('/transactions', '[email protected]')->name('client.transaction.index'); 
    Route::get('/transactions/{id}/show', '[email protected]')->name('client.transaction.show'); 
    Route::get('/transactions/{id}/export', '[email protected]')->name('client.transaction.export'); 

}); 

有一些測試路線,評論每個角色等等。 我的路線應該如何? 任何幫助將不勝感激。

回答

1

沒有已知的最佳實踐,但它取決於您的應用程序和您的偏好。對於更大的應用程序,爲了輕鬆管理數百條路由聲明,您可以使用不同的方法,但使用最簡單的方法。關於你的問題,以下是我在2013/2014管理在5.0/5.1一個更大的應用途徑經驗:

到現在爲止(前Laravel 5.3),我已經成功我在不同的文件路徑,例如,我的主要app\Http\routes.php文件看起來像這樣:

<?php 

# Front End 
require_once('Routes/frontend.php'); 

# Auth (AuthController Routes) 
require_once('Routes/auth.php'); 

# Admin Back End 
require_once('Routes/admin.php'); 

現在,很明顯的是,我已經建立在我的Http文件夾和我的應用程序的每個部分Routes文件夾中,我有一個簡單的PHP文件,例如,admin.php文件中app\Http\Routes文件夾,看起來像這樣:

<?php # Routes/admin.php; 

$router->group([ 
    'prefix' => 'admin', 
    'namespace' => 'Admin', 
    'middleware' => 'acl' 
], function() use ($router) { 

     $router->get('dashboard', [ 
      'as' => 'dashboard', 
      'uses' => '[email protected]', 
      'permission' => OWN_ACCOUNT, 
      'log_description' => 'Landed on Dashboard.', 
      'bread' => ['dashboard' => 'Dashboard'], // [routename => label] 
      'menuItem' => ['icon' => 'fa fa-dashboard', 'title' => 'Dashboard'] 
     ]); 


     require_once('Admin_Routes/user.php'); 
     require_once('Admin_Routes/role.php'); 
     require_once('Admin_Routes/tag.php'); 
     require_once('Admin_Routes/help.php'); 

     // Many More... 
}); 

正如你所看到的,Routes/admin.php文件還包括從被稱爲Admin_Routes另一個文件夾,其中包含所有其他路徑的文件與http:\\domain.com\admin爲基礎URI也是這些路由其他一些文件需要一個許可檢查,以便中間件acl在組內部使用,而不是所有特定的路由聲明。

所以,這樣我可以將我的路線prefixmiddleware分組,並且它更有組織。因爲它是一個巨大的應用程序,所以我需要一種方法來清理分離我的路線。

現在,在這個組中,每個包含的(require_once)文件也將具有相同的URI前綴和中間件鉤子,並且我不需要在路由聲明中附加這些,例如,app\Http\Routes\Admin_routes\help.php文件包含某些內容像這樣:

$router->group(['namespace' => 'Help', 'prefix' => 'help'], function() use ($router) 
{ 
    $router->get('/', [ 
     'uses' => '[email protected]', 
     'as' => 'help.lists', 
     'permission' => 'manage_help', 
     'bread' => ['dashboard' => 'Dashboard', 'help.lists' => 'Manage Help'], 
     'menuItem' => ['icon' => 'fa fa-question-circle', 'title' => 'Manage Help'] 
    ]); 

    # More .... 
]); 

其結果是,我不需要再添加此組中的acl並注意URI幫助裏面的路線聲明,這是'/',並從我的應用程序訪問此路線,我需要導航到http://domain.com\admin\help,但我做了組wie,所以我不需要再輸入完整的URI,這條路線也會通過acl中間件檢查,因爲我已將它附加到父組中。另外,我的help.php文件僅包含與我的HelpController相關的路線,該路線易於管理(可讀)。


注:請忽略航路聲明中permission, log_description, bread, menuItem鍵不熟悉的東西,這些都是我自己的實現來管理情侶取決於我的應用需求的東西,不出來的盒子Laravel,不得適合所有情況。