在 Laravel 中将课程放在哪里?

wen*_*zel 5 php laravel laravel-5.7

我正在做我的第一个项目,试图学习 Laravel,我已经到了想要创建一个对象的地步。

我已经创建并试用了它,它可以按我的意愿工作,但是我应该把它放在哪里?到目前为止,它直接位于我的控制器中,但感觉不对,而且我认为它弄乱了代码。你实际上应该把它放在哪里?有这样的地方吗?

这就是我的代码的外观,正如您所看到的,它被称为“主机”,它位于页面顶部:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


class Host {
    public $ipv4, $mac;

    public function __construct($ipv4, $mac){
        $this->ipv4 = $ipv4;
        $this->mac = $mac;
    }
}

class PagesController extends Controller
{
    public function index(){

        libxml_use_internal_errors(true); //ignore invalid HTML tag warnings
        $dom = new \DOMDocument();
        // Check that there's actually a file to load 
        if(!$dom->loadHTMLFile('\\\192.168.1.201\root\test.xml')){
            die('error loading xml');
        }

        // Loop through all <host> tags
        foreach ($dom->getElementsByTagName('host') as $hostKey => $host) {
            $hostAttributes = array();
            // Check for <address> tags and loop through them as well
            foreach ($host->getElementsByTagName('address') as $addressKey => $addressValue) {
                // Check that there is an <addrtype> and <addr> tag
                if($addressValue->getAttribute('addrtype') && $addressValue->getAttribute('addr')){
                    // Put that into the array $hostAttributes
                    $hostAttributes[$addressValue->getAttribute('addrtype')] = $addressValue->getAttribute('addr');
                }
            }
            // Check for the keys 'ipv4' and 'mac' in $hostAttributes
            if(array_key_exists('ipv4', $hostAttributes) && array_key_exists('mac', $hostAttributes)) {
                $hosts[$hostKey] = new Host($hostAttributes['ipv4'], $hostAttributes['mac']);
            }
        }

        // set data
        $data = [
            'hosts' => $hosts
        ];

        // return view
        return view('pages.index')->with('data', $data);
    }
}
Run Code Online (Sandbox Code Playgroud)

该文件位于“/app/Http/Controllers/PagesController.php”中,我正在运行 Laravel 5.7.21

Sak*_*lam 6

您应该使用namespaces并创建一个对您和您的要求有意义的命名空间和目录结构。例如,您可以在目录中创建一个名为HelpersBusinessLogic或其他任何内容的app目录。然后把你的类放在适当的命名空间中,例如目录Helpers,命名空间是App\Helpers.

这是在 composer PSR 4 自动加载中设置的。结帐自动加载在PHPPSR 4在这些文章


使用 PSR-4 自动加载的示例

将类放置在以下结构中

  • 应用程序
    • 帮手
      • 主机.php

然后将其导入您的控制器类中

<?php

namespace App\Http\Controllers;

use App\Helpers\Host;
use Illuminate\Http\Request;

class PagesController extends Controller
{
...
...
            if(array_key_exists('ipv4', $hostAttributes) && array_key_exists('mac', $hostAttributes)) {
                $hosts[$hostKey] = new Host($hostAttributes['ipv4'], $hostAttributes['mac']);
            }
...
...    

Run Code Online (Sandbox Code Playgroud)


Mat*_*own 5

如果有帮助,我通常会像这样构建我的 Laravel 应用程序:

  • 应用程序
    • 核心 (共享应用程序范围的东西)
      • 安慰
        • 内核.php
        • 命令
          • 一些命令.php
      • 网址
        • 内核.php
        • route.php (拉入所有路由文件)
        • 中间件
          • ... (Laravel 中间件)
        • 要求
          • ... (所有核心请求基类)
        • 控制器
          • 控制器.php
      • 服务
        • APIService.php
      • 供应商
        • 应用服务提供者.php
        • ... (其余的 Laravel 提供者)
      • 例外
        • 处理程序
      • 存储库
        • EloquentRepository.php
      • helpers.php(通常我这里有一个 Helpers 文件)
    • (业务逻辑的东西)
      • 用户
        • User.php (用户模型)
        • 网址
          • 路由文件
          • 控制器
            • 用户控制器.php
        • 要求
          • 新用户请求.php
    • 部门
      • Division.php (另一个模型)
      • 网址
        • 路由文件
        • 控制器
          • 域控制器.php

显然,为了简洁起见,我省略了一些内容,但您明白了。

我的 PSR-4 定义最终看起来像:

"autoload": {
  ...
  "files": [
    "app/Core/helpers.php"
  ],
  "psr-4": {      
    "App\\": "app/Domain/",
    "Core\\": "app/Core/"
  }
  ...
}
Run Code Online (Sandbox Code Playgroud)

像这样修改 Laravel 的结构还需要您bootstrap/app.php使用新的命名空间更新您的文件,以及您从默认 Laravel 安装中移动的任何文件。

使用上面的结构并根据这个新对象的作用,你应该很清楚你把它放在哪里。您甚至可以在User下为该类创建一个Models文件夹。或者,假设它与用户相关,只需将新类直接放在User.php模型旁边。

它可能看起来像这样:

<?php namespace App\User;

class SomeClassName {
  ...
}
Run Code Online (Sandbox Code Playgroud)

然后,例如,从UserController.php引用可能如下所示:

<?php namespace App\User;

use Core\Http\Controllers\Controller;
use App\User\SomeClass;

class UserController extends Controller {

    public function __constructor(SomeClass $someClass)
    {
        $this->someClass = $someClass; 
    }
}
Run Code Online (Sandbox Code Playgroud)

所有假设,但希望让你指向正确的方向。