douxian5076 2019-01-25 14:31
浏览 37
已采纳

Laravel的课程在哪里?

I'm doing my first project trying to learn Laravel and I've come to the point where I want to create an object.

I've created it and tried it out and it works as I want it to, but where do I put it? As of now it lies directly in my controller, but it doesn't feel right and besides that I think it messes up the code. Where are you actually supposed to put it? Is there such a place?

This is how my code looks and as you can see it is called "Hosts" and it's placed at the top of the page:

<?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.201oot\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);
    }
}

The file is located in "/app/Http/Controllers/PagesController.php" and I'm running Laravel 5.7.21

  • 写回答

2条回答 默认 最新

  • duanhan7001 2019-01-25 14:38
    关注

    You should be using namespaces and create a namespace and directory structure that makes sense to you and your requirements. For example, you can make a directory named Helpers or BusinessLogic or anything else in the app directory. Then put your classes there with proper namespace e.g. for directory Helpers, the namespace is App\Helpers.

    This is set up in composer PSR 4 autoloading. Checkout Autoloading in PHP and PSR 4 in these articles


    Example using PSR-4 Autoloading

    Place class in the following structure

    • app
      • Helpers
        • Host.php

    Then import it in your controller class as

    <?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']);
                }
    ...
    ...    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题