0

我最近潛入laravel世界(版本5.4)。雖然最初感到困惑,但MVC的概念在編寫大型應用程序時很有意義。您想讓外部開發人員輕鬆理解的應用程序。組織控制器laravel

爲此,使用laravel大大簡化了PHP中的編碼並使語言再次變得有趣。然而,除了將代碼分成相應的模型,視圖和控制器之外,如果我們需要劃分控制器以防止它們變得太大,會發生什麼?

我發現這個解決方案是爲每個文件夾定義一個控制器,然後用控件的特性進一步添加功能來填充該控制器。 (全部大寫=文件夾):

CONTROLLER 
    HOME 
     Controller.php 
     TRAITS 
      additionalFunctionality1.php 
      additionalFunctionality2.php 
      additionalFunctionality3.php 
      ... 
    ADMIN 
     Controller.php 
     TRAITS 
      additionalFunctionality1.php 
      additionalFunctionality2.php 
      additionalFunctionality3.php 
      ... 

routes/web.php我woud初始化一切像這樣:

Route::namespace('Home')->group(function() { 
    Route::get('home', '[email protected]'); 
    Route::post('test', '[email protected]'); 
    Route::post('test2', '[email protected]'); 
    Route::post('test3', '[email protected]'); 
}); 
Route::namespace('Admin')->group(function() { 
    Route::get('Admin', '[email protected]'); 
    Route::post('test', '[email protected]'); 
    Route::post('test2', '[email protected]'); 
    Route::post('test3', '[email protected]'); 
}); 

與我是新來laravel,這似乎是一個簡單而優雅的方式來組織我的邏輯。然而,這是我在研究laravel控制器組織時沒有看到的東西。

問題

是否有一個問題,無論是在短期和長期來看,組織我的數據是這樣的?什麼是更好的選擇?

示例控制器:

<?php 

namespace App\Http\Controllers\Message; 

use DB; 
use Auth; 
use Request; 
use FileHelper; 

use App\Http\Controllers\Message\Traits\MessageTypes; 
use App\Http\Controllers\Controller; 

class MessageController extends Controller 
{ 
    // Traits that are used within the message controller 
    use FileHelper, MessageTypes; 

    /** 
     * @var array $data Everything about the message is stored here 
     */ 
    protected $data = []; // everything about the message 

    /** 
     * @var booloean/array $sendableData Additional data that is registered through the send function 
     */ 
    protected $sendableData = false; 

    /** 
    * Create a new controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware('auth'); 
     $this->middleware('access'); 
    } 

    /** 
     * Enable sendableData by passing data to the variable 
     * 
     * @param array $data Addition data that needs to registrered 
     * @return MessageController 
     */ 
    protected function send ($data = []) { 
     // enable sendableData by passing data to the variable 
     $this->sendableData = $data; 

     return $this; 
    } 

    /** 
     * Enable sendableData by passing data to the variable 
     * 
     * @param string $type The type of message that we will serve to the view 
     * @return MessageController 
     */ 
    protected function serve ($type = "message") { 
     $this->ss(); 
     $this->setData(array_merge($this->sendableData, $this->status[$type])); 
     $this->data->id = DB::table('messages')->insertGetId((array) $this->data); 
    } 

    /** 
     * Set the data of the message to be used to send or construct a message 
     * Note that this function turns "(array) $data" into "(object) $data" 
     * 
     * @param array $extend Override default settings 
     * @return MessageController 
     */ 
    protected function setData(array $extend = []) { 
     $defaults = [ 
      "lobby" => Request::get('lobbyid'), 
      "type" => "text", 
      "subtype" => null, 
      "body" => null, 
      "time" => date("g:ia"), 
      "user" => Auth::User()->username, 
      "userid" => Auth::User()->id, 
      "day" => date("j"), 
      "month" => date("M"), 
      "timestamp" => time(), 
      "private" => Request::get('isPrivate') ? "1" : "0", 
      "name" => Request::get('displayname'), 
      "kicker" => null 
     ]; 
     $this->data = (object) array_merge($defaults, $extend); 

     // because a closure can not be saved in the database we will remove it after we need it 
     unset($this->data->message); 

     return $this; 
    } 

    /** 
     * Send out a response for PHP 
     * 
     * @return string 
     */ 
    public function build() { 
     if($this->data->type == "file") { 
      $filesize = @filesize("uploads/" . $this->data->lobby . "/" . $this->data->body); 
      $this->data->filesize = $this->human_filesize($filesize, 2); 
     } 
     // do not send unneccessary data 
     unset($this->data->body, $this->data->time, $this->data->kicker, $this->data->name, $this->data->timestamp); 

     return $this->data; 
    } 

    /** 
     * Send out a usable response for an AJAX request 
     * 
     * @return object 
     */ 
    public function json() { 
     return json_encode($this->build()); 
    } 

} 

?> 
+0

你使用laravel的最新版本嗎? – GBCrafty

+0

不知道你爲什麼試圖將Laravel與MVC聯繫起來。它與朝鮮民主主義人民共和國與民主的關係與MVC大致相同。也就是說,如果你需要拆分和重組控制器,那麼你已經泄漏了太多的UI和業務邏輯。 –

+0

@GBCrafty是的,我是sir – Shawn31313

回答

0

,我認爲你應該做的有點不同!首先,你應該因爲性狀不控制器在同一水平控制器使用的特質,你的樹應該更像:

Http 
    Controller 
    Controller.php 
    Home 
     YourControllers 
    Admin 
     Your admin controllers 
    Traits 
     Your Traits 

下一頁你的路線需要更多這樣的:

Route::group(['prefix' => 'home'], function() 
{ 
    Route::get('/', 'Home\[email protected]')->name('home.index'); 
} 



Route::group(['prefix' => 'admin', 'middleware' => ['admin']], function() 
{ 
    Route::get('/', 'Admin\[email protected]')->name('dashboard.index'); 
} 

您可以使用許多扭結或路線,如:

Route::post('/action', '[email protected]')->name('controller.store'); 
Route::patch('/action', '[email protected]')->name('controller.update'); 
Route::resource('/action', 'yourController'); 

資源自動路由使用最多的爲您創建,像後,補丁,編輯,索引..你只需要編寫的行動和控制器調用它的動作。你可以用這個命令查看你的頭像:php artisan route:list

Laravel還有許多自動功能,比如用這個命令創建一個控制器:php artisan make:controller YourController。

對於這些路由,前綴會創建url的一部分,例如,路由組中的前綴爲'admin'的所有路由都會像www.yourwebsite.com/admin/theroute一樣,並且也可能被阻止用戶使用中間件。

爲了熟悉laravel,我建議您從Laracasts的Jeffrey Way的頭開始學習laravel 5.4教程,他非常擅長解釋和展示laravel的工作原理。這裏有一個鏈接:https://laracasts.com/series/laravel-from-scratch-2017

希望它有幫助,問我,如果你想知道什麼或有一些精度,我會盡力回答你!

+0

您是否建議將特質文件夾分爲相應的家庭和管理員特徵? – Shawn31313

+0

如果你的項目不是一個有很多特質的大項目,你就會迷路。但是,如果你想要,你可以做,如果你覺得它是有用的和必要的! – GBCrafty

0

Laravel體系結構對於任何大小的應用程序都足夠簡單。

Laravel爲開發人員提供了多種機制來解決應用程序中的脂肪控制器問題。

  • 使用Middlewares進行驗證。使用Requests進行驗證和操作數據。
  • 使用Policy爲您的應用角色。
  • 使用Repository來編寫數據庫查詢。
  • 使用Transformers爲您的APIs轉換數據。

這取決於您的應用程序。如果它太大並且具有不同的模塊或功能,那麼您應該使用模塊化方法。 一個很好的包可用於製作獨立模塊here

希望這有助於。