How to Generate Controller in Symfony 3.0? - symfony

Hope you are all fine, I am a beginner trying to learn Symfony 3.0, so I want to generate my controller in a folder called Bundles, but it is not created, here is the command I taped :
php bin/console generate:controller
And this is fine, when I tape the controller name, that was what I wrote :
?[32mController name?[39m: Bundles/FrontBundle
But their answer is :
?[37;41m The controller name must contain a : ("Bundles/FrontBundle"
given, expecting something like AcmeBlogBundle:Post) ?[39;49m
I don't know if I have to do something before this to noy have such error.
Any Help would be much appreciated.

This is because controller name should be a combination of BundleName:ControllerName and you have provided a relative path to your bundle. If that doesn't work make sure you have your autloading properly setup and Symfony knows about your bundle

According to his doc you should use the shortcut notation when you are declaring a controller, entity, etc.
also you need to read the output of the command.
Welcome to the Symfony controller generator
Every page, and even sections of a page, are rendered by a controller.
This command helps you generate them easily.
First, you need to give the controller name you want to generate. You
must use the shortcut notation like AcmeBlogBundle:Post
Controller name: BundleNameEndingBundle:ControllerName

Related

Symfony2 - Compiler Passes

I want to change the value of a parameter located in parameters.yml from my controller
with
$this->container->setParameter('myParam','newValue');
I get an error : Impossible to call set() on a frozen ParameterBag
I really looked for a solution but I have not understood how compile passes work
does someone can give me a simple example
UPDATE:
I have some parameters stored there (my mailbox, the LAPD parameters, etc ..) I would like to give control to the admin to configure it from the dashboard

Symfony: Dynamic configuration file loading

Here is the context :
Each user of my application belongs to a company.
Parameters for each company are defined inside "company.yml" configuration files, all of them sharing the exact same structure.
These parameters are then used to tweak the application behavior.
It may sound trivial, but all I'm looking for is the proper way to load these specific YAML files.
From what I understood so far, using an Extension class isn't possible, since it has no knowledge about current user.
Using a custom service to manage these configurations rather than relying on Symfony's parameters seems more appropriate, but I can't find how to implement validation (using a Configuration class) and caching.
Any help would be greatly appreciated, thanks for your inputs!
Using the Yaml, Processor and Configuration components of Symfony2 should fit your needs.
http://symfony.com/doc/current/components/yaml/introduction.html
http://symfony.com/doc/current/components/config/definition.html
Define your "CompanyConfiguration" class as if you were in the DependencyInjection case
Create a new "CompanyLoader" service
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Config\Definition\Processor;
$companies = Yaml::parse('company.yml');
$processor = new Processor();
$configuration = new CompanyConfiguration();
$processor->processConfiguration($configuration, $companies);
Now you should be able to use your companies array to do what you want
Have a look at http://symfony.com/doc/current/cookbook/configuration/configuration_organization.html as well as http://symfony.com/doc/current/cookbook/configuration/environments.html. If that's not the correct answer you'll have to be more specific on what your company.yml configuration contains.

Symfony2 - resize image after upload

I want to generate thumbnails after I upload the file. For image manipulations, I use Avalanche123's ImagineBundle.
I tried using the code in Entity:
$avalancheService = $this->get('imagine.cache.path.resolver');
$avalancheService->getBrowserPath($this->getUploadRootDir().'/'.$path.'/'.$extn[0].'.jpg', 'avatar');
But it doesn't help. What can I do?
On this line you try to get a service: $this->get('imagine.cache.path.resolver').
But a code example you're following is supposed to be executed in a controller. There is no get() method in an Entity, controller inherits it from Controller class that all controllers should extend. So calling $this->get() in an Entity is pointless.
This is by design. Entities in Symfony are supposed to be dumb and only represent data they have.
The right thing to do is to either do resize in your controller, or create a service, inject imagine.cache.path.resolver into it and call it from your controllers.
Check the variables using dump(var) in twig. Perhaps $extn[0] doesn't have the correct file or isn't set. Try poutputting the full getBrowserPath string to make sure it's accurate.

use generated entities as "base class" in sf2/doctrine2

How can I tell Doctrine2 Entity Manager to use an inherited class by default, instead of the generated one?
In my new sf2 app, I generated all entities from the existing database to /src/Package/Bundle/DataBundle/Entity - of course I'd like to have generated forms as well, so I need for my foreign-key relations __toString() methods, which I don't want to put into those generated files because they get overwritten (yes they also get backuped in an extra file, so my changes aren't lost, but manually merging files is not what I want).
So I added new classes to /src/Package/Bundle/DataBundle/Model inheriting all from the entities, but with a __toString() method and surely some other tweaks in the future as well. But now, when I call $entity = $em->getRepository('PackageDataBundle:Customer')->find($id); - I get an instance of /src/Package/Bundle/DataBundle/Entity/Customer instead of /src/Package/Bundle/DataBundle/Model/Customer ..
I'd like to have the behaviour from sf1, where all custom work is done in the inherited classes and the generated ones are the "base" Classes, which can be updated any time on a schema update and aren't touched otherwise..
I hope there is some configuration for this..
Maybe as a bonus, I'd like to have my naming convention turned around: to have Model as the "abstract" generated one and Entity as the actual used one
Thanks.
I'd like to have the behaviour from sf1, where all custom work is done in the inherited classes and the generated ones are the "base" Classes, which can be updated any time on a schema update and aren't touched otherwise..
For purposes you described you should use Propel ORM - it was so in Symfony-1.4, and became even more flexible for Symfony-2.0 with Propel 1.6. It is well documented, easily installed and naturally used within Symfony-2.0
Perhaps I misunderstood your problem, but going from Propel to Doctrine i was also disappointed that there is no way to keep that "dirty" auto-generated code out of my logic.
I do not know how much it is now important to you, but I currently writing a simple bundle that implements such generation style.
$ app/console doctrine:generate:entities СompanySomeBundle --propel-style=true
Generating entities for bundle "СompanySomeBundle"
> backing up User.php to User.php~
> generating Сompany\SomeBundle\Entity\Base\User
> generating Сompany\SomeBundle\Entity\User
https://github.com/madesst/MadesstDoctrineGenerationBundle
PS: Sorry for my english =/

best and right way toorganize bundles in symfony2

I'm a little bit confused how I should organize the bundles in symfony2. In my app I'll need 3 pages:
1- Insert
2- List
3- Update
Which one could be the right and best way to organize my code?
src/cp/AddPageBundle
src/cp/EditPageBundle
src/cp/UpdatePageBundle
OR
In one bundle write 3 different controllers, each one in a different file?
OR
In one bundle, write 3 different actions in one controller file?
I'm really confused with this.
Thanks in advance!
In one bundle, 3 different actions in one controller. There is no need to split this functionality across bundles or controllers.
For example you can edit/list/update User with UserController (insertAction, ListAction, UpdateAction) to deal with the user and for example add another controller (CommentsController) for edit/list/update comments. The same situation can be used for your Page example (add/edit/update)
Optional way is to create folder inside controller folder so that we have even more organized code. For example for create Admin folder for controllers: Admin/ConsoleController, Admin/CategoryController, Admin/PluginController to deal with admin functionality.
Exactly I agree with TroodoN-Mike. Also you should create PageBundle with your Page entity and your fields (publish_date, title, content ect), and execute
app/console generate doctrine:crud PageBundle:Page
Symfony will generate a basic CRUD, but with your insert/list/update
just tried this - command is:
app/console doctrine:generate:crud --entity PageBundle:Page
This starts up a wizard on the command line which will prompt you for any other parameters it needs.
Unfortunately only works if your primary key field is called id and has a getID() method.

Resources