Symfony "use" for another namespace? "question

I have a problem with namespaces in connecting my entity class to an abstract type class.

I need to create a AbstractTypeclass I called BlogFactory . I intend to use it in my function of createActionmy BlogController.php Entity class to create blog entries.

My BlogFactoryclass is created in the Form / Blog directory of my package, as the tree structure shows.

.
.
.
src/Blogger/BlogBundle/
β”œβ”€β”€ Controller
β”‚   β”œβ”€β”€ BlogController.php
β”‚   └── PageController.php
β”œβ”€β”€ DataFixtures
β”‚   └── ORM
β”‚       └── BlogFixtures.php
β”œβ”€β”€ Entity
β”‚   β”œβ”€β”€ Blog.php
β”‚   β”œβ”€β”€ Blog.php~
β”‚   └── Enquiry.php
β”œβ”€β”€ Form
β”‚   β”œβ”€β”€ Blog
β”‚   β”‚   β”œβ”€β”€ BlogFactory.php
β”‚   β”‚   └── Factory.php
β”‚   └── EnquiryType.php
β”œβ”€β”€ Resources
β”‚   β”œβ”€β”€ config
β”‚   β”‚   β”œβ”€β”€ config.yml
β”‚   β”‚   β”œβ”€β”€ routing.yml
β”‚   β”‚   └── services.yml
.
.
.

In the entity class, BlogController.phpI include the following use command :

namespace Blogger\BlogBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Blogger\BlogBundle\Form\Blog\BlogFactory;

class BlogController extends Controller{
.
.
.

with the following function createAction:

public function createAction(){   
        $form = $this->createForm(new BlogFactory(), new Blog(), 
            array(
                'action' => $this->generateUrl('blog_create'),
                'method' => 'POST',
            )
        );

        $form->add('submit', 'submit', array('label'=>'Create'));
        return $form;
    }

Here is the code in the class BlogFactory:

namespace Blogger\BlogBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class BlogFactory extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('title');
        $builder->add('author');
        $builder->add('blog');
        $builder->add('image');
    }

    public function getName()
    {
        return 'newblog';
    }
}

However, my problem is that I get the following error:

The autoloader expected class "Blogger\BlogBundle\Form\Blog\BlogFactory" to be defined in file "/var/www/Symfony-Blog/src/Blogger/BlogBundle/Form/Blog/BlogFactory.php". The file was found but the class was not in it, the class name or namespace probably has a typo

, .

UPDATE:

Gnucki, Blogger\BlogBundle\Form\Blog; BlogFactory.php Blogger\BlogBundle\Form\Blog\BlogFactory; BlogController.php, :

Attempted to load class "Blog" from namespace "Blogger\BlogBundle\Controller".
Did you forget a "use" statement for another namespace?
+4
1

:

, BlogFactory Form/Blog/BlogFactory.php, Blogger\BlogBundle\Form.

BlogFactory:

namespace Blogger\BlogBundle\Form\Blog;

:

use Blogger\BlogBundle\Form\Blog\BlogFactory;
use Blogger\BlogBundle\Entity\Blog;

:

PHP- require include . . . Symfony2 , . , Blogger\BlogBundle\Entity\Blog /Blogger/BlogBundle/Entity/Blog.php.

( (, )), , :

new \Blogger\BlogBundle\Entity\Blog();

use Blogger\BlogBundle\Entity\Blog;

new Blog();

, , :

namespace Blogger\BlogBundle\Controller;

new Blog();

Blog Blogger\BlogBundle\Controller\Blog.

namespace Blogger\BlogBundle\Controller;

use Blogger\BlogBundle\Entity\Blog;

new Blog();

, , Blog Blogger\BlogBundle\Entity\Blog.

. PHP .

+11

Source: https://habr.com/ru/post/1569248/


All Articles