Reputation: 51
I have a project that I am developing in Symfony2. I have a PHP class "buildComponent" that I wrote that builds all of my form elements. 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. I get an error "Attempted to load class "buildComponent" from the global namespace. Did you forget a "use" statement?".
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".
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.
I have also tried clearing cach and installing web assets.
I have been developing with Symfony2 for about a month and this is the first real issue I have ran in to. 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.
<?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
<?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
Upvotes: 0
Views: 8874
Reputation: 51
Wow. Finally. 26 hours later I finally got this working. 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". I moved my formComponents.php script to this directory.
I then created the namespace for "formComponents.php"
namespace Solutions\SolutionsBundle\Model
Now in my contactUs.html.php page I can just declare the use.
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".
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.
Upvotes: 1