我最近潜入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