我最近潛入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());
}
}
?>
你使用laravel的最新版本嗎? – GBCrafty
不知道你爲什麼試圖將Laravel與MVC聯繫起來。它與朝鮮民主主義人民共和國與民主的關係與MVC大致相同。也就是說,如果你需要拆分和重組控制器,那麼你已經泄漏了太多的UI和業務邏輯。 –
@GBCrafty是的,我是sir – Shawn31313