2

I am struggling to include an external class the Laravel way. I have a round-about way of doing this right now..

First I have a file app/Http/Requests/Moneris/Moneris.php with the following content:

<?php
namespace App\Http\Requests\Moneris;

class mpgTransaction {
/* stuff */
}

Then in app/Http/Controllers/MyController.php, I have the following content:

<?php
namespace App\Http\Controllers;

require_once(str_replace("/MyController.php","/",__FILE__).'../Requests/Moneris/Moneris.php');
use App\Http\Requests\Moneris as mn;

class MyController extends Controller
{
    public function index()
    {
        $mpg = new mn\mpgTransaction();
    }
}

And this works fine. But if I change the content of MyController.php to:

<?php
namespace App\Http\Controllers;

use App\Http\Requests\Moneris\Moneris;

class MyController extends Controller
{
    public function index()
    {
        $mpg = new mpgTransaction();
    }
}

I get a 500 internal server error and the logs say mpgTransaction class could not be found. What am I doing wrong?

2 Answers 2

3

To follow PSR-1 standard you should name your class as Moneris instead of mpgTransaction. Also for the autoloader to work, you must have file name and class name match.

So the file Moneris.php should be.

<?php
namespace App\Http\Requests\Moneris;

class Moneris {
/* stuff */
}

In Laravel you don't use require_once(), use PSR-4 to autoload, therefore you should remove it when you follow the autoloader rules, which i think you will when you rename the class.

Your final controller version can then be.

use App\Http\Requests\Moneris\Moneris;

public function index()
{
    $mpg = new Moneris();
}
Sign up to request clarification or add additional context in comments.

2 Comments

oH thanks, the Moneris is actually the SDK that Moneris gave me, and it has about 15 other classes there all starting with lowercase. I wonder if that will cause problems later on....
Found similar SDK's for that code and i'm sad to say that is way below industry standard for a package. Just use your first hack and don't try to fix to much :) or make the changes your self locally.
3

There are three type of namespace

  1. unqualified
  2. qualified
  3. fully qualified

In your first case,you include that file and namespace was qualified namespace- that's why it was work fine. But in your second case, you just violated the autoload standard PSR-4

The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.

It will work fine,if you just give class name Moneris instead of mpgTransaction in your Moneris.php file.

How to use use

// Pre PHP 7 code
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;

use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;

use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC;

// PHP 7+ code
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};

Note: Namespace names are case-insensitive.

Important link

namespaces basics

2 Comments

Thank you for explaining the different types of name spaces. It will help me in future. IN the mean time, I don't want to rename mpgTransaction to Moneris because mpgTransaction is just one of many classes in the file as part of the Moneris SDK that I downloaded. I don't feel comfortable right now rewriting the SDK for Moneris.
than you might change Moneris.php file name to MpgTransaction.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.