找不到 Laravel 5.1 PHP DOMDocument() 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31577588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.1 PHP DOMDocument() class not found
提问by user3023925
I'm using PHP's built-in DOMDocument() class to do some simple web-scrapping. However, it works on my 4.2 site, but not my 5.1 (both are on same installation of PHP).
我正在使用 PHP 的内置 DOMDocument() 类来做一些简单的网页抓取。但是,它适用于我的 4.2 站点,但不适用于我的 5.1(两者都在相同的 PHP 安装中)。
Here's the error:
这是错误:
Class 'App\Http\Controllers\III_Ranks\DOMDocument' not found
找不到类“App\Http\Controllers\III_Ranks\DOMDocument”
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()
我认为这是一个命名空间问题,但我不知道如何访问 DOMDocument()
Thanks for any help.
谢谢你的帮助。
回答by GeorgeQ
In Laravel 5.1 you must prefix the class name with the global namespace prefix '\'.
在 Laravel 5.1 中,您必须使用全局命名空间前缀“\”作为类名的前缀。
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...
}
}
回答by Hyder B.
For me, in order to solve the error: DOMDocument() class not found
. I had to install the DOM extension.
对我来说,为了解决错误:DOMDocument() class not found
. 我必须安装DOM 扩展。
If you are using PHP 5:
如果您使用的是 PHP 5:
You can do so on Debian / Ubuntu using:
您可以在 Debian / Ubuntu 上使用:
sudo apt-get install php5-dom
And on Centos / Fedora / Red Hat:
在 Centos / Fedora / Red Hat 上:
yum install php-xml
If you are using PHP 7:
如果您使用的是 PHP 7:
For Ubuntu:
对于 Ubuntu:
apt-get install php7.0-xml
For CentOS / Fedora / Red Hat:
对于 CentOS / Fedora / Red Hat:
yum install php70w-xml
回答by Phoenix404
Tested in Laravel 5.4
测试在 Laravel 5.4
you can use keyword use
:
您可以使用关键字use
:
In top of file before define the class you can write use DOMDocument;
instead of new \DOMDocument();
在定义类之前的文件顶部,您可以编写use DOMDocument;
而不是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 '...' ?
这是什么 '...' ?
回答by Radames E. Hernandez
I had this problem in laravel 5.6on a server with ubuntu 16.04, and I solved this issue installing the xmlpackage for php7.1
version, this does not work for me in the php7.2
version I don't know why.
我在带有ubuntu 16.04的服务器上的laravel 5.6中遇到了这个问题,我解决了这个问题,安装了版本的xml包,这在我不知道为什么的版本中对我不起作用。php7.1
php7.2
1.- Make sure you have the version 7.1 of php installed, I use this command to select the correct php version in ubuntu:
1.- 确保您安装了 php 7.1 版本,我使用此命令在 ubuntu 中选择正确的 php 版本:
update-alternatives --set php /usr/bin/php7.1
Note:In case you don't have installed the php7.1
version, do you need to install it before the above command, in that case you can use this command: apt-get install php7.1
注意:如果你没有安装php7.1
版本,是否需要在上面命令之前安装它,在这种情况下你可以使用这个命令:apt-get install php7.1
2.- To install the package XML I was used the following command:
2.- 要安装包 XML,我使用了以下命令:
apt-get install php7.1-xml
3.- Restart your apache server, in my case with this command:
3.- 重新启动您的 apache 服务器,在我的情况下使用以下命令:
service apache2 restart
I hope to be helpful, this saved my day.
我希望能有所帮助,这拯救了我的一天。
回答by Krishnamoorthy Acharya
I had the same issue with Amazon Linux 2 (LAMP) with laravel framework
我在使用 laravel 框架的 Amazon Linux 2 (LAMP) 上遇到了同样的问题
Please find below steps resolve issues:
请找到以下步骤解决问题:
sudo systemctl stop httpd
sudo yum install php-xml
sudo systemctl start httpd