Where do put classes in Laravel?
Asked Answered
D

2

8

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.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);
    }
}

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

Digastric answered 25/1, 2019 at 14:31 Comment(2)
As best practice, you should limit your classes to 1 per file. In doing so, you make things like autoloading, naming etc much simpler, while also following the SRP principle. What are you using the class Host for?Asgard
I see. The class Host is used to create objects containing ip-adresses and mac-adresses of connected devices to my home network. One object per device. These objects are then stored in an array called hosts.Digastric
B
12

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']);
            }
...
...    

Baddie answered 25/1, 2019 at 14:38 Comment(4)
I followed your suggestion and ended up with this: 1. Created a helpers folder with host.php in it. 2. Put my class along with namespace App\Helpers; in it. 3. Put this in the top of my controller use App\Helpers\Host; 4. Added this to to my composer.json under autoload "files": [ "app/Helpers/ConnectedHost.php" ], . It seems to be working now, but maybe I could autoload the whole Helpers directory instead of each file in composer.json?Digastric
@wenzzzel, you are not using PSR-4, that's why you need to use the files autoload. A point (simplified) to be noted here, Your class name and filename should be the same (and case-sensitive) for composer to figure out where and how to autoload it. In your case, the directory is Helpers, your namespace will be App\Helpers, your filename will be Host.php in the app\Helpers folder, and you import it by use App\Helpers\Host; in your controller. Then you won't need the files entry in your composer.json. I hope this makes sense.Baddie
@Digastric , I added a short example to make it clear.Baddie
I already did all that, just did'nt realize that PSR-4 did autoload it. Tried removing the files: entry under autoload: inside composer.json and it still works. Thank for clearing that out for me! :)Digastric
R
8

If it's helpful, I usually structure my Laravel apps like this:

  • app
    • Core (Shared App Wide Stuff)
      • Console
        • Kernel.php
        • Commands
          • SomeCommand.php
      • Http
        • Kernel.php
        • routes.php (Pulls In All Route Files)
        • Middleware
          • ... (The Laravel Middleware)
        • Requests
          • ... (All Core Request Base Classes)
        • Controllers
          • Controller.php
      • Services
        • APIService.php
      • Providers
        • AppServiceProvider.php
        • ... (The Rest Of the Laravel Providers)
      • Exceptions
        • Handler.php
      • Repositories
        • EloquentRepository.php
      • helpers.php (Usually I Have A Helpers File Here)
    • Domain (Business Logic Stuff)
      • User
        • User.php (User Model)
        • Http
          • routes.php
          • Controllers
            • UserController.php
        • Requests
          • NewUserRequest.php
    • Divisions
      • Division.php (Another Model)
      • Http
        • routes.php
        • Controllers
          • DomainController.php

Obviously I'm leaving some stuff out for brevity, but you get the idea.

My PSR-4 definition ends up looking like:

"autoload": {
  ...
  "files": [
    "app/Core/helpers.php"
  ],
  "psr-4": {      
    "App\\": "app/Domain/",
    "Core\\": "app/Core/"
  }
  ...
}

Modifying Laravel's structure like this also requires you update your bootstrap/app.php file with the new namespaces, along with any files you move from the default Laravel install.

Using the above structure and depending on what this new object will do it should be pretty clear to you where you'd put it. You could even just make a Models folder for the class under, say, User. Or, just put the new class directly next to the User.php model, assuming it's related to users.

It might look something like this:

<?php namespace App\User;

class SomeClassName {
  ...
}

Then, referencing from, say, the UserController.php might look like:

<?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; 
    }
}

All hypothetical, but hopefully gets you pointed in correct direction.

Redman answered 25/1, 2019 at 16:2 Comment(6)
I like this approach and i find it very manageable in the long run. I like the idea of multiple namespacing. I am not sure if you can call them services but i would even assign different services to within the domain entities.Zigzagger
Since I'm new to both Laravel and the MVC concept I'm not sure I understand every bit of what you're saying, but this is what I believe I understood: I could simply put my hosts class inside a helpers.php file which I put right inside my app folder. This helpers file could contain multiple classes that are commonly used throughout the application. I would then reference it like this: use App\Helpers\Host; from , in my case, PagesController.php. Did I get that right?Digastric
mmm, I'd reserver helpers.php for function type helpers. Put your class in it's own file. Definitely learn about namespacing if you're not already familiar.Redman
I see what you're saying now. You could do that, yea. I usually end up with a 'helpers.php' that just holds one-off function type helpers. If you're going to have an app wide class I'd probably end up naming it UtilityHelper.php or something, but I think you're on the right track.Redman
Thanks for explaining it for me!Digastric
Good idea to keep model files and business logic classes in Domain folders, but Laravel offers to keep models in Models folder as a best practice.Apodaca

© 2022 - 2024 — McMap. All rights reserved.