Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 PHP DOMDocument() class not found

I'm using PHP's built-in DOMDocument() class to do some simple web-scraping. However, it works on my 4.2 site, but not my 5.1 (both are on same installation of PHP).

Here's the error:

Class 'App\Http\Controllers\III_Ranks\DOMDocument' not found

Here's my controller:

<?php

namespace App\Http\Controllers\III_Ranks;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Illuminate\Http\Response;

class RanksController extends Controller 
{
    public function getRanks()
    {
        $list1 = new DOMDocument();
        //etc...
    }
}

I figure this is a namespace issue, but I have no idea how to access DOMDocument()

Thanks for any help.

like image 225
user3023925 Avatar asked Jul 23 '15 03:07

user3023925


3 Answers

In Laravel 5.1 you must prefix the class name with the global namespace prefix '\'.

So your updated code:

<?php

namespace App\Http\Controllers\III_Ranks;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Illuminate\Http\Response;

class RanksController extends Controller 
{
    public function getRanks()
    {
        $list1 = new \DOMDocument();
        //etc...
    }
}
like image 63
GeorgeQ Avatar answered Oct 22 '22 09:10

GeorgeQ


For me, in order to solve the error: DOMDocument() class not found. I had to install the DOM extension.

If you are using PHP 5:

You can do so on Debian / Ubuntu using:

sudo apt-get install php5-dom

And on Centos / Fedora / Red Hat:

yum install php-xml

If you are using PHP 7:

For Ubuntu:

apt-get install php7.0-xml

For CentOS / Fedora / Red Hat:

yum install php70w-xml
like image 10
Hyder B. Avatar answered Oct 22 '22 10:10

Hyder B.


Tested in Laravel 5.4

you can use keyword use :

In top of file before define the class you can write use DOMDocument; instead of new \DOMDocument();

For eg:

<?php

namespace App\Http\Controllers\III_Ranks;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Illuminate\Http\Response;

use DOMDocument;

class RanksController extends Controller {
...

what is this '...' ?

like image 5
Phoenix404 Avatar answered Oct 22 '22 10:10

Phoenix404