I want to generate some CRUD controllers with the following command:
php app/console generate:doctrine:crud
my problem is that I made 2 bundles (1 front end and 1 for the admin section) my entities are in the defaultbundle, but I'd like to generate the CRUD controllers in my admin bundle, is there a way to do this?
Generating CRUD controllers in a bundle different of the entity's one is, as far as I know, impossible.
Indeed, CRUD controllers generator is just here for test. But if you really want to use it, you can actually copy the controller, views and form in the other bundle. You'll just need to change the templates' paths and the namespaces at the beginning of the controller and the Form type.
Related
I'm starting to develop a project that will be quite big (hear lots of files) and I want to have an organization that's different from Symfony's default one: instead of having one dir for all my controllers, another for all my forms, etc, I want to have one dir per functionality, ie a directory for my homepage which will contain the controller, the templates, another dir for the blog page with the controller, forms and templates, and so on.
I tried what was explained in this (old) answer, and it didn't work : my routes weren't recognized, and a php bin/console debug:router showed they weren't listed anymore.
I feel I will have something to adapt in my routes.yaml file, which looks like this for now, and which explains why it doesn't work because it explicitely searches in the src\Controller directory:
controllers:
resource:
path: ../src/Controller/
namespace: App\Controller
type: attribute
I looked in the documentation I found, but I didn't find anything useful (I think I will have to remove the path and change it to something else, but I don't know what yet.
The above solutions are only for differentiating controller and template files. But For Entity, Events, and Forms it will not work.
It seems like you want to separate files for each module or functionality. For example, settings.yaml, controller, entity, forms, events, etc...
For that Symfony provides a Bundle feature. You can create a new Bundle in Symfony and use it as a separate feature. For more details please read this link
What is better practise, to use annotations in Controllers or list of routes in file(s) (e.g. routes.yaml) and more importantly, Why?
I worked on big project, where you had all routes in yamls sorted by categories and every time you created new one, you had to update at least controller and some of files. I did not like it. I am hopefully starting one project and trying to decide what better option. I am considering using annotations, but I do not have enough experience yet to be sure it is right approach.
In my opinion, both are good options, and there are not an absolute truth about it. You should use the one with which you feel most comfortable.
For me, the main difference between them are:
Annotations
Keep simple the process of read and update a route, since the route and controller are in the same file, very close to each other.
You're combining in the same file controllers and routing configurations.
YAML
More difficult to read; each time you need to check the route or params, you need to look for the correct yaml file.
More organised way and separated concepts.
My final preference is to go with annotations, the main reason for that is because I don't like the yaml format at all.
It all depends
for common and simple routes to your AppBundle i suggest annotations, But for other bundles that you might want to reuse i like yaml, but the standard is xml. The reason is that the user of the third party bundle can copy the yaml/xml file, and place it in its appbundle and then he can change it and add his own version to his routing. A nice example is fosUserBundle. Imagine that you dont want a registration form because only the administrator may add new users. In that case you dont want routes to registration and would have to change route configuration
Dynamic routes
Sometimes you also need dynamic routes. SonataAdmin is a bundle that generates dynamic routes. It adds routes for each service that is tagged with sonata.admin.
I am creating an app that will be extended by several bundles. Users will have access to different bundles based on roles. Some of these bundles will have configuration options, and I want one page with all config forms.
What I want to do is create a page that every bundle will "hook" into, and show the configuration form if the bundle has one.
There will also be a dashboard page that each bundle should "hook" into and show a dashboard widget.
Is there any way of achieving this in symfony? And if so, how?
I finally understood the The DependencyInjection Component so I guess the answer lies there. In detail, I will try to create an event in the configuration controller, and all bundles will have a subscriber to this event, and somehow have the forms available for the configuration twig file. But that is another question.
I 'm a new with Symfony2!. Can you tell me please if I can user CRUD with FOSUserBundle?? Is that even possible ? Actually, i want create a Manager role who can edit and delete users form database !
FosUserBundle doesn't provide such functionality. You can generate CRUD using Symfony standard edition built in task:
php app/console doctrine:generate:crud
for your User entity.
For more complex purposes you need to check some admin bundles, maybe SonataAdminBundle . This will provide admin generator in which you can manage (CRUD) your entities.
Also note, that using all kinds of code generators is considered as not-so-good-practice
For those who are facing this issue :
- if you override the parent classe with custom fields (let's say "firstname" + "lastname") in you User entity
- and then re-generate the CRUD
you will see that the 2 custom field became editable.
The Doctrine CRUD generator looks incompatible with FOSUserBundle in the present state, as we create inheritance with a User master class.
There is a same issue if you try to use the Group Model of FOSUserBundle.
I want to learn is it possible to add additional folder to Controller folder. My reason is pretty simple: I want to divide my project administration and client sides.
Example: I have a controller named Post that has actions Index, Details, Delete, Create, Edit. I want to make one controller as user controller that will consist of Index, Details and another controller as admin controller that will consist of Delte, Create, Edit. Then I will be able to easy distinguish what is what and put admin validation on whole admin class.
Another reason is that I want my url for administrating my site to look like /admin/post/delete, not /post/delete.
So is it possible, and if so then what would be the best way to implement this?
Sound like you want to use MVC Areas? http://www.c-sharpcorner.com/UploadFile/b19d5a/areas-in-Asp-Net-mvc3/
It's just a convention on placing controllers in Controllers folder.
Actually MVC finds controller in current loaded assemblies.
You can place them even in other assemblies.
So, fell free to create additional folders inside Controllers
If you are using Ruby on Rails, yes, you can. In your routes files, config/routes.rb, add this:
map.namespace :admin do |admin|
admin.resources :posts
end
Go to your terminal and navigate to your project, run rake routes. Now you get your posts controller under admin namespace... and your url will be:
.../admin/posts