简体   繁体   English

Symfony2尝试从全局名称空间加载类。 您是否忘记了“使用”声明?

[英]Symfony2 Attempted to load class from the global namespace. Did you forget a “use” statement?

I have a project that I am developing in Symfony2. 我有一个正在Symfony2中开发的项目。 I have a PHP class "buildComponent" that I wrote that builds all of my form elements. 我有一个PHP类“ buildComponent”,它可以构建所有表单元素。 Everything works fine when I run the application/website on my dev machine and everything works fine in prod mode uploaded to my web host except for the "buildComponent" class. 当我在开发机器上运行应用程序/网站时,一切工作正常,并且在以prod模式上载到Web主机上的一切工作正常,除了“ buildComponent”类。 I get an error "Attempted to load class "buildComponent" from the global namespace. Did you forget a "use" statement?". 我收到错误消息“尝试从全局名称空间加载类“ buildComponent”。您是否忘记了“ use”语句?”。

Initial Script(Works in dev mode|Doesn't work in prod mode) 初始脚本(在开发人员模式下有效|在生产模式下无效)

include $view["assets"]->getUrl("php/formComponents.php");
global $formComponents;
$formComponents = new buildComponent();

I tried declaring namespace but got an error that the declaration had to be at the beginning of the script. 我尝试声明名称空间,但收到一个错误,该声明必须在脚本的开头。 I assumed I would make the call in my Controller but a namespace was already declared so I thought I could declare "use". 我以为可以在Controller中进行调用,但是已经声明了名称空间,所以我认为可以声明“ use”。

I tried declaring "use" at the beginning of the above script and in the controller but I still get the load class error. 我尝试在上述脚本的开头和控制器中声明“使用”,但仍然收到加载类错误。 I also tried "require_once" all without any success. 我也尝试了“ require_once”,但都没有成功。

I have also tried clearing cach and installing web assets. 我还尝试过清除缓存并安装Web资产。

I have been developing with Symfony2 for about a month and this is the first real issue I have ran in to. 我已经用Symfony2开发了大约一个月,这是我遇到的第一个实际问题。 I am sure it is a newbie issue and I apologize if it's something simple. 我敢肯定这是一个新手问题,很抱歉,这很简单。

The formComponents.php script is located @ web/php/formComonents.php. formComponents.php脚本位于@ web / php / formComonents.php。

<?php

class buildComponent{

    public function buildElement($id){

        return 'Example';

    }

}

?>

The page that is trying to call the class is located @ src/Solutions/SolutionsBundle/Resources/views/pages/contactUs.html.php 试图调用该类的页面位于@ src / Solutions / SolutionsBundle / Resources / views / pages / contactUs.html.php

<?php $view->extend('::default.html.php') ?>
/*contactUs.html.php*/
<?php
/*Throwing Class not found error.*/
include $view["assets"]->getUrl("php/formComponents.php");
global $formComponents;
$formComponents = new buildComponent();

?>


    <!--Document Specific Stylesheets and Scripts-->
<?php $view['slots']->start('header') ?>
    <link href="<?php echo $view['assets']->getUrl('css/formComponents.css') ?>" rel="stylesheet" />
<?php $view['slots']->stop() ?>




    <!--Document Body-->
<?php $view['slots']->start('body') ?>

    <div class="inputContainer">
        <?php echo $formComponents->returnInputTitle('Create Username/ID'); ?>
        <?php echo $formComponents->returnTextField('txtUserID', 'txtUserID','text','Create Username/ID'); ?>
    </div>

<?php $view['slots']->stop() ?>




    <!--Document Specific Javascript-->
<?php $view['slots']->start('jscript') ?>

<?php $view['slots']->stop() ?>

Stack Trace 堆栈跟踪

[1] Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "buildComponent" from the global namespace.
Did you forget a "use" statement?
    at n/a
        in /home/blubyrdstudios/public_html/solutions/src/Solutions/SolutionsBundle/Resources/views/pages/contactUs.html.php line 7

Wow. 哇。 Finally. 最后。 26 hours later I finally got this working. 26小时后,我终于开始工作了。 It's not really what I want but it's logical as far as keeping with Re-Usable Code and Bundles. 这并不是我真正想要的,但就保持可重用代码和捆绑软件而言,这是合乎逻辑的。

In my Bundle "Solutions\\SolutionsBundle\\" I created a directory "Model". 在我的捆绑包“ Solutions \\ SolutionsBundle \\”中,我创建了一个目录“ Model”。 I moved my formComponents.php script to this directory. 我将formComponents.php脚本移至该目录。

I then created the namespace for "formComponents.php" 然后,我为“ formComponents.php”创建了名称空间。

namespace Solutions\SolutionsBundle\Model

Now in my contactUs.html.php page I can just declare the use. 现在,在我的contactUs.html.php页面中,我可以声明使用。

use Solutions\SolutionsBundle\Model\formComponents as formBuilder;
global $buildForms;
$buildForms = new formBuilder();

The naming of the files is slightly different from what I posted in my questions. 文件的命名与我在问题中发布的文件略有不同。 The reason is that I read somewhere that the name of the file containing the class had to be = to the name of the class.(Still trying to find some documentation on this to confirm it) So I changed the class name from "buildComponent" to "formComponents". 原因是我在某处读取到包含该类的文件的名称必须是=到该类的名称。(仍试图在此找到一些文档以确认它),所以我从“ buildComponent”更改了类名称。到“ formComponents”。

Now everything seems to be running fine. 现在一切似乎运行良好。 If anyone has a better solution or an additional solution that would work I would love to see it. 如果有人有更好的解决方案或其他可行的解决方案,我很乐意看到它。 Thanks for your help. 谢谢你的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从全局命名空间加载 class “DOMDocument”。 您是否忘记了“使用”声明? - Attempted to load class "DOMDocument" from the global namespace. Did you forget a "use" statement? 尝试从全局命名空间加载类“Memcached”。 您是否忘记了“使用”语句? - Attempted to load class “Memcached” from the global namespace. Did you forget a “use” statement? 试图从全局名称空间中加载类“ Thread”。 您是否忘记了“使用”声明? - Attempted to load class “Thread” from the global namespace. Did you forget a “use” statement? 尝试从全局命名空间加载类“PHPUnit_Framework_TestCase”。 您是否忘记了“使用”声明? - Attempted to load class "PHPUnit_Framework_TestCase" from the global namespace. Did you forget a "use" statement? 试图从命名空间加载类。 您是否忘记了另一个命名空间的“use”语句? 使用供应商 php 命名空间 - Attempted to load class from namespace. Did you forget a "use" statement for another namespace? with vendor php namespace 尝试从命名空间“Symfony\\WebpackEncoreBundle”加载类“WebpackEncoreBundle”。 您是否忘记了另一个命名空间的“use”语句? - Attempted to load class "WebpackEncoreBundle" from namespace "Symfony\WebpackEncoreBundle". Did you forget a "use" statement for another namespace? 尝试从命名空间“DoctrineExtensions \ Query \ Mysql”加载类“Month”。您是否忘记了另一个命名空间的“use”语句 - Attempted to load class “Month” from namespace “DoctrineExtensions\Query\Mysql”. Did you forget a “use” statement for another namespace 尝试从命名空间加载类是否忘记了另一个命名空间的“use”语句? - Attempted to load class from namespace did you forget a “use” statement for another namespace? 尝试从命名空间“knp \\ Bundle \\ PaginatorBundle”加载类“knpPaginatorBundle”。你忘记了另一个命名空间的“use”语句吗? - Attempted to load class “knpPaginatorBundle” from namespace “knp\Bundle\PaginatorBundle”.Did you forget a “use” statement for another namespace? Symfony 3:原则列表操作:您是否忘记了另一个名称空间的“使用”语句? - Symfony 3: Doctrine listAction: Did you forget a “use” statement for another namespace?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM