Mapping Exception when trying out the Symfony2 example from the cookbook - php

I just started learning Symfony2 and I'm following the examples from "The Cookbook" from Symfony's website.
When trying the code from the chapter about loading users from database (Entity Provider) (Link to the chapter) I get the following error:
MappingException: Class Acme\UserBundle\Entity\User is not a valid entity or mapped super class.
... and can't find out wthat I am doing wrong. I do think I followed all the steps provided in the chapter.
Thanks for any help,

I finally found the problem when revising step by step the whole code.
I forgot to register UserBundle in AppKernel.php.

I have the same problem. I looked at symfony+Mapping error but that solution not works for me. Then I found, that Michi solution works https://stackoverflow.com/a/10935672/2910183
So, here is what I do at all:
register bundle in AppKernel.php:
new Acme\UserBundle\AcmeUserBundle(),
create this bundle (it is just a copy of FOS\UserBundle\FOSUserBundle) and save as src/Acme/UserBundle/AcmeUserBundle.php
<?php
namespace Acme\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass;
use Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass;
class AcmeUserBundle extends Bundle {
}

Related

In MakeCrud.php line 103: Call to a member function getRepositoryClass() on null

I'm trying to create a crud in Symfony 4.4 with bin/console make:crud command but I'm getting the following error
In MakeCrud.php line 103:
Call to a member function getRepositoryClass() on null
this is part of the class I'm trying to use for the crud
<?php
namespace Domain\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
class Segment
{
private $id;
private $uidentifier;
private $name;
I type the class name with the namespasce oto genterate crud but I get the error, what could be wrong?
So I got a little bit curious and confirmed that it is a namespace issue.
I made myself a Domain\Entity\Segment class and then ran
bin/console make:crud 'Domain\Entity\Segment'
And got the same error message.
The make:crud command expects entities to live under App\Entity. Does not seem to be any way to convince it to look under Domain\Entity just for entities.
However the Maker Bundle itself allows you to change the name of the App's root namespace:
# config/packages/maker.yaml Need to add this file
maker:
root_namespace: Domain
# Then run
bin/console make:crud Segment
And it works. The problem is that the root namespace is used for generating all the files so, for example, you end up with a Domain\Controller\SegmentController class which is probably not where you want them to be.
I checked the source and there is no easy way to adjust just the entity namespace. If you plan on using crud quite a bit then you either need to copy your entities to App\Entity or specify the root_namespace and then copy/refactor your generated controllers and forms. Either way it will be a pain.
I suppose you could also fork the maker bundle and edit the source code directly. Might actually be worth the effort if you have a bunch of crud to generate. But that will be left as an exercise.

Sonata Block Bundle is having trouble finding my new class

I am trying to create a new block service using Sonata Block Bundle. It is running atop Symfony 3.3.
I have cloned my existing TextBlockService.php file into a new file called CenteredOverlineBlockService.php. Both files reside in the src/AppBundle/Resources/config/ directory.
I have added the following text to my blocks.yml file:
vgms.block.centeredoverline:
class: AppBundle\Block\CenteredOverlineBlockService
arguments:
- "Centered Overline"
- "#templating"
- "#sonata.media.manager.media"
- "#sonata.admin.pool"
tags:
- { name: sonata.block }
... and I have added the following reference in sonata_block.yml:
vgms.block.centeredoverline:
... and I now get the following error:
Attempted to load class "CenteredOverlineBlockService" from namespace
"AppBundle\Block". Did you forget a "use" statement for another
namespace?
So my question is: What step am I missing here?
The application loads the previously existing TextBlockService without complaint. It's only this new service that seems to create a problem.
===
Edit #1: Here is the top of the class involved:
namespace AppBundle\Block;
use ...
class CenteredOverlineBlockService extends \Sonata\BlockBundle\Block\Service\TextBlockService
{
...
For what it's worth, my "fix" was to clone a service from a different bundle and then use that class instead.
That doesn't really answer the "why" question I posed, but maybe it will hopeuflly help someone.

Symfony4: No route found for "GET /lucky/number"

I am starting to play with symfony4. I've just created new application and create new LuckyController. It works with routes.yaml configured in this manner:
lucky:
path: /lucky/number
controller: App\Controller\LuckyController::number
With the following controller:
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
class LuckyController
{
public function number()
{
return new Response('<html><head></head><body>' . rand(111, 999) . '</body></html>');
}
}
But I want to use annotations. So I decided to comment routes.yaml. Following documentation that explain how to create a route in symfony I've made this:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class LuckyController extends Controller
{
/**
* #Route("/lucky/number")
*/
public function number()
{
return new Response('<html><head></head><body>' . rand(111, 999) . '</body></html>');
}
}
Sometimes this problem occurs because of cache.you need to run php bin/console cache:clear.then it will work fine.
In Symfony4 you have to install annotations bundle.
Run this command composer require annotations
Then restart Your project.
I had the same thing when trying the annotations, then I found out with the demo installed (the blog) you need to add the language in the URL. So the documentation says:
http://localhost:8000/lucky/number will work exactly like before
That did not work. This however did:
http://localhost:8000/en/lucky/number
As suggested in #Jack70 answer.
https://127.0.0.1:8000/en/random/number
This should work.
If you run the command php bin/console debug:router it would show you the list of routes available as you can see in that list, you need to add the locale (language) as first argument.
app_annotationsample_number ANY ANY ANY /{_locale}/random/number
If you use annotations - you need check config/routes/anotations.yaml.
You need check path to your controller or add new path, for example:
controllers:
resource: ../../src/Controller/
type: annotation
Also, you'll get that error if the route name appears more than once.
I ran into that error while developing Angular app, so in order to debug I tried that same route in Postman, which I knew used to work fine, but this time it did not throwing the mentioned error. Luckily, I have just added one new controller, so finding the culprit was easy: the problem was that after copying methods from web controller to api controller I forgot to change route name in one of the methods.
So you might wanna be careful when copying :)

Symfony Flex: Override FOSUser login form

I read up on two ways to override the FOSUser login form:
place template file in app/Resources/ Docs
override the Controller in a child Bundle Docs
However, with Symfony Flex the project structure changed substantially.
As for option #1: There is no app/Resources folder anymore. I store my templates in /templates.
Regarding option #2: There is no AppBundle anymore.
Now, I extended FOSUser's security Controller:
<?php
namespace App\Controller;
use FOS\UserBundle\Controller\SecurityController as BaseController;
class SecurityController extends BaseController
{
public function renderLogin(array $data)
{
return $this->render('/Admin/Login/index.html', $data);
}
}
The application still renders the FOSUserBundle form. I have a feeling that I have to register the avobe controller somehow. In the FOSUser config, there is no directive for that. How can this be achieved?
In Symfony 4, you just need put the file in /templates/bundles/FosUserBundle
Hope it helps.
I think you followed the good path but forgot to change your config. You need to tell fosuser to use a different form file.
Check this file, line 106 & 109 :)
I didn't check this solution with flex, but a friend of mine did with symfony 4 and it worked just fine.

How to access Orchestra Xml Parser through Laravel IoC Container

I am using laravel 5.1. I want to use XML parser and I have searched and found Orchestra as mostly being used. So I have gone thorough all the steps given at documentation to install and configure. I have added Orchestra\Parser\XmlServiceProvider::class in providers section of config/app.php and 'XmlParser' => Orchestra\Parser\Xml\Facade::class in aliases section.
Now in my controller, I have added its name space like use Orchestra\Parser\Xml\Facade; at the top of my controller. But when i try to use its function in my action, like
$xml = XmlParser::load($xml_document);
It generates error stating,
Class 'App\Http\Controllers\XmlParser' not found
So I want to know is there any other way in Laravel 5.1 to use the packages and I am doing some thing wrong with Orchestra if some one has used it.
Since the documentation already describe registration of the facade alias:
'XmlParser' => Orchestra\Parser\Xml\Facade::class,
You can either use \XmlParser::load(), or import the alias.
use XmlParser;
or import the full namespace.
use Orchestra\Parser\Xml\Facade as XmlParser;
It looks as though it's searching inside the controllers for it..
Class 'App\Http\Controllers\XmlParser' not found
Therefore:
$xml = XmlParser::load($xml_document);
Needs to be:
$xml = \XmlParser::load($xml_document);
Should resolve this problem
In Laravel 5.1 the controller is in a namespace. The XmlParser is in an other namespace. You need to include that namespace in your controller.
<?php
namespace Orchestra\Parser\Xml; // Maybe this one is different
class Controller...
You can also add a \ to make it work.
$xml = XmlParser::load($xml_document);

Categories