I have been coding in SYmfony since last 6 months now. And after completing a couple of projects, I started exploring symfony in greater details. While exploring i came accross, TreeBuilder component. Now, i understood the concept TreeBuilder very well. Its basically used to dump config files in YML. But, the question is, why would one use TreeBUilder when you can directly modify the condig yml files.? Is there any other better use case where one should use TreeBUilder specificaly.? Could you please help me understand any use case. This will give me a better understanding.
According to documentation Tree builder is a component which can/should be used to validate configuration provided by end user.
Configuration values are usually expected to show some kind of
hierarchy. Also, values should be of a certain type, be restricted in
number or be one of a given set of values. For example, the following
configuration (in YAML) shows a clear hierarchy and some validation
rules that should be applied to it (like: "the value for auto_connect
must be a boolean value")
So basically you can make a set of requirements to configuration of your bundle like
required
default value
type constraint
For complete reference check documentation
Related
As far as I know, currently Yii 2 doesn't have an out of the box method to resolve ambiguity of controller and module names. An example of module hierarchy to describe what exactly I mean:
app\modules\v1\controllers\UserController // resolves the /v1/users and /v1/users/{id} actions
app\modules\v1\modules\user\Module.php // nested module, resolves the /v1/user/... controllers and their actions, e.g. /v1/user/something/{id}
In this case, the UserController conflicts with the user Module. The main reason of the ambiguity is the singular-plural magic of Yii 2 framework. I didn't find an appropriate solution to resolve this ambiguity. Further my ideas how to resolve it.
Rename the module.
Rename the UserController to the UsersController.
Create an additional submodule, and place there the UserController. E.g. app\modules\v1\modules\root\controllers\UserController
I'm not sure that at least one of these options is a quite elegant one and a proper solution in view of the Yii 2 philosophy.
Coming back to the main question, what is a more appropriate approach to resolve this issue by the Yii 2 philosophy? Controller and Module is two different types of objects, which is differently pluralized or not, thus should be right way to separate them in the routing for the described case.
How I usually deal with this depends a bit on how I'm structuring things.
Let's say I have users, departments, orders and maybe some other stuff. All these concepts have their own module in which all interactions take place. If I want to create REST controllers for all these concepts I can choose to add a controller to every module or I can create a dedicated API module for the controllers.
When creating a dedicated module for this purpose I usually name it api, maybe with some nested versioning module(s) inside. So in this situation I would get the following structure app\modules\api\controllers\UserController which would result in the URL /api/user. No ambiguity there and pretty clear what this is meant for.
When adding such a controller to the module itself I choose a better name than just 'UserController'. When asking myself the question 'What does this controller accomplish?/What does it do?' I find that just UserController doesn't cut it; Especially when inside a User module, resulting in /user/user. This controller is probably going to exist alongside one or more different controllers in the User module, all meant for something different. So, usually, I end up naming it just ApiController, resulting in /user/api. Another controller could be the ProfileController. So when looking at the URLs it's pretty clear what /user/api and /user/profile do. Without the ambiguity.
I am not sure I can fully understand the question, but probably you're asking about classname aliases?
use My\Full\Classname as Another;
I recently decided to take a look at Sylius, since I love the idea of a developer-friendly Symfony2 project.
I tried to look through the various documentation articles, but I didn't seem to be able to find the answer for a very fundamental question that I have: what is the recommended way to start development on a new web-store, which will include (at the very least), the ability to implement one's own HTML template designs, and still be able to easily upgrade Sylius to future versions?
The best approach that I was able to come up with is to create a new bundle (in my case, named WebBundle) which is based on the default SyliusWebBundle. Here's the problem. In order to get the bare minimum of allowing Sylius to use the templates in my bundle, rather than the default one, I had to go through many hoops. Here are several things I have done so far:
Copied the contents of the original Controller directory from SyliusWebBundle. Changed return values to use WebBundle rather than SyliusWebBundle a part of the string in the argument to $this->render(), as well as the class namespaces.
Copied the YAML files in the Resources/config/routing directory from SyliusWebBundle to my bundle. Changed SyliusWebBundle references in the YAML files, similar to the above.
Added new sections to app/config/config.yml, specifically this part (intended to override the contents of addCheckoutSection() in Sylius\Bundle\CoreBundle\DependencyInjection\Configuration):
sylius_core:
# ...
checkout:
steps:
security:
template: 'WebBundle:Frontend/Checkout/Step:security.html.twig'
addressing:
template: 'WebBundle:Frontend/Checkout/Step:addressing.html.twig'
shipping:
template: 'WebBundle:Frontend/Checkout/Step:shipping.html.twig'
payment:
template: 'WebBundle:Frontend/Checkout/Step:payment.html.twig'
finalize:
template: 'WebBundle:Frontend/Checkout/Step:finalize.html.twig'
I have a lot more work in changing all the default controller references in the YAML files in Resources/config/routing/frontend directory, but before I proceed onward, I need to know if this is the correct approach, or if I'm going down the wrong path.
My goal is to make the store as easy to upgrade as possible with new releases of Sylius, so I'd like to avoid modifying core library files, and instead selectively overriding functionality using my own bundles, as needed.
However, Sylius currently doesn't yet appear to be "geared" towards this approach, unless I missed something.
The fact that I had to override functionality from more than one bundle (CoreBundle as well as WebBundle, per the above YAML section), made me pause with my current approach. I hope someone might be able to steer me in the right direction.
you can override all templates in the app folder (this is part of symfony and works with all bundles):
app/Resources/SyliusWebBundle/views/Frontend/Checkout/Step/
security.html.twig
addressing.html.twig
shipping.html.twig
payment.html.twig
finalize.html.twig
Using CakePHP 2.x
I have successfully generated many models, controllers, and views but one of them is just not working.
the database table is name 'server_cpu', The model appears to generate fine as I have compared it to other models that can be turned into controllers and views and it is identical. It also does have the useTable = 'server_cpu', but even still when I try to generate the Controller it tells me that the model has to have a table. After looking closely I believe that it is trying to use the table 'server_cpues', How can i force it to use 'server_cpu' and not 'server_cpues', note that I have tried emptying the /tmp/cache/ folder and that has no effect.
The error when attempting to generate a controller for 'ServerCpus' using cake bake: 'You must have a model for this class to build basic methods. Please try again.'
There are two possible solutions:
Firstly: simply changing the name of the table can resolve this problem, but it should be noted that for many this is not a possibly depending on the stage of development, for example if the current database is well established and used by many other systems or application this may not be possible. If you are starting from scratch this will be an easier solution.
Secondly: a slightly more complex solution would be to work with Inflectors to change the behavior of CakePHP. This can be done by modifying the file '/app/Config/bootstrap.php' to add a custom Inflector, for documentation on this refer to this for information on inflectors for CakePHP 2.x. For this particular situation you could use something like
Inflector::rules('plural', array('rules' => array( '/(.*)cpu$/i' => '\1Cpu' ) ));
Note the use of regex to recognize all string containing cpu
I wonder where can I get more information about special syntax like #somevar or %somevar% in symfony2's yaml configuration?
For example, using # defines a call to a service, that is how we pass dependencies to services. %somevar% on the other hand refers to the value of an already defined parameter with the name somevar.
So, if I do:
parameters:
custom: "some value"
another: %custom%
then another will be populated with the value of custom, which in my case is "some value". My question is, where are these relations documented?
My particular need is to be able to reference an element of an array, something like %somevar[somekey]%, but that syntax doesn't work.
Thanks in advance!
EDIT: I found this: Full merge key support.
Full support for references, aliases, and full merge key. Don't repeat yourself by referencing common configuration bits.
in the yaml docs, but no furthur documentation about it..
What you are searching for is not really about Yaml itself, but about the Yaml loader of the Dependency Injection container.
If you search docs about it, here are the ones for the old component (v1): http://components.symfony-project.org/dependency-injection/trunk/book/05-Service-Description
Symfony2 comes with a new component (based on the same principles). You can find the official docs here: http://symfony.com/doc/current/book/service_container.html#service-parameters
Concerning your problem, you cannot access to keys of DI parameters, you have to flatten then manually.
You could use a DI extension to fit your need, take example on some bundles like: https://github.com/symfony/AsseticBundle/blob/master/DependencyInjection/AsseticExtension.php#L54 (maybe not the best example).
I am using Symfony 1-4 and sfDoctrineGuardPlugin.
My question is, on Doctrine Guard Plugin as you know each module came with generator.yml. And i need to customize generator.yml. For example, i need to display another table's (module's) column.
// for example i am at X module and need to sort according to another table's/module's column
...
config:
list:
sort: [X, asc] // x is not on my module
Same thing with list/display. I need to display some column which is not on my current module...
I couldn't find it on the web. Thanks a lot for sharing your idea and/or information, erman.
Never mind to customize any file directly in the plugin's dir.
Override generator.yml by copying it into your application's module. The module must be the same name as in plugin. See "Anatomy of a Plug-in".
Symfony does not provide a built-in functionality to sort items by columns that don't present in current model. You have to do it manually by overriding an addSortQuery method in your actions class. See "Symfony 1.4 admin generator sort on custom column".
Hope this would help.