How access to database on routing file with Symfony2? - php

I have to manage a multilingual routing as part of a Symfony2 project.
In order to get the whole URL translated i have to access the EntityManager from the PHP routing file to get the proper translation and the translated slugs.
Does anyone know how to do that ?
Thanking you,
Antoine.

May be you have to make separate third-level domain for each language (ru.site.com, fr.site.com) and generate routes using your database translation table from template?

What I could suggest you is to check the JMSI18nRoutingBundle. This bundle let you define localized routes. Here ta copy of the overview text for the bundle taken from the documentation:
Overview
This bundle allows you to create i18n routes. Key points:
uses the Translation component; translate URLs just like you would translate any other text on your website
allows you to use different hosts per locale
does not require you to change your development processes
can translate all routes whether they are coming from third-party bundles, or your own application
I did not use it myself and I'm not the developer of this bundle and I don't know if it will work for your slug. But I hope this will help you in some ways.
Regards,
Matt

Related

Multilingual Symfony2 website

I have a huge problem that I couldn't resolve in the last few days. I don't expect you to give me a final solution because I think the problem is not so simple.
So I have a huge Symfony2 application that was built with the idea to work only with one language. All routes are in their responsible controllers. Now I want to prefix all routes with the selected language so could have:
myapp.com/en/news
myapp.com/fr/news
etc...
And if it is possible the default language should not be shown in the route - if English is my default language I want only myapp.com/news. The problem is that I can't just go through all routes and make them work both with optional parameter about the language and also work without that parameter. If it is not possible I am ok en to be presented in the route too.
I tried the jmsi18nroutingbundle because many other people suggested it but when I setup the config.yml as it is said only some of the routes get prefix when I check them with app/console debug:router. And because of that I am either not configuring it well or it has some limitations.
My question is how do you handle multilingual websites and their routes - do you start with the idea about multilingual system from the beginning and create at least two routes for each action one with optional parameter about the language and one without that parameter or you have more global solution to handle all routes?

Laravel localization, redirect to the right view

I'm using laravel and mcamara/laravel-localization package to manage a bilingual site (french/english).
In the package documention https://github.com/mcamara/laravel-localization
they don't explain how to point an URL to the right view depending on which language is set as local.
For example, if the URL is /en/contact load the view en.contact (en is the subdirectory for views written in english) and id the URL is /fr/contact load the view from the subdirectory fr.
The same for the URL that needs translation /en/products for english needs to load /fr/produits when the local language is set to french.
Disclaimer: I have never used the package...
That's because the package (and also the localization of laravel) assume that you are using Laravel's translations. So the package basically only worries about setting the application locale right and Laravel does the rest of the job.
However if you need different views (or don't want to use translations) you can just retrieve the current locale and use it to build the name of the view
View::make(App::getLocale().'.contact');
You have to change the view-path dynamically, so you dont have to prefix your views by hand.
Just create a new Middleware and register it in Kernel.php.
I've created a gist for that.
https://gist.github.com/vanderb/80dea9a8039dd60962c5cb291eae7b75
Now, all you have to do is to reference the view-name, without adding the lang-code every time.
I also recommend to use named-routes
https://laravel.com/docs/5.4/routing#named-routes
I don't see why you would want to use different views for each language. That would double your code. Just use one view with Laravels own translation features to translate your strings. The package handles the naming of routes (/products vs /produits). The views should stay the same.
If you really want different views for different languages, #lukasgeiter has the answer. Sounds like a code-duplication, though.
Side-note: Laravel 5.4s new JSON translation feature together with langman is really great.

Language routing in Silex

I'm looking for a solution where I can routing in Silex different URLs to a controller to get more organized my code.
The problem is that I need to route to the same controllerProvider some routes in different languages:
$app->mount("/{_locale}/string-in-english", new App\Controllers\myController());
$app->mount("/{_locale}/string-in-spanish", new App\Controllers\ myController());
Where /string-in-english and /string-in-spanish are routed to the same controller.
Most likely, the possible routes are inserted into a table in a database.
Greetings and thanks in advance.
Full i18n routing is tricky, you will need to use a translation service and a single call to a mount() that can handle translatable routes.
Take a look to this article. There are some interesting comments and the author published an I18nRoutingServiceProvider.
In my opinion Silex rocks when used in simple apps, but it can become really hard when you start to add this kind of features. Sometimes it is easier to implement them in a Symfony app.

Application structure in Symfony 2

I am quiet new to the symfony framework and took some lessons and purchased the 'Starting in Symfony2' tutorial from knpuniverse. I want to be sure that I use the correct setup for my application. My question is, How do you call your first central bundle? eg. FrontendBundle? I want to make the next structure in my application:
FrontendBundle
The front where people get a landing site where they can also login
from there, when they login, they get into the next bundle:
CustomerBundle
Backend app where customers get their invoices and pay them and edit their information we stored in the database
And at least:
AdminBundle
Another backend app where I can edit customers, make invoices for customers and edit the app information
Is this the correct way and is FOSUserBundle a good bundle for this kind of application?
Bundles have sense only if they can be used in "multiple projects". I mean: if you write code, make it as a bundle and you can't reuse that bundle (for example because bundles, as you described above, are strictly and logically connected to your project) this three bundle separations is totally useless.
So, final answer (that is also a question) is:
Could you reuse FrontendBundle in other projects?
Could you reuse CustomBundle in other projects?
Could you reuse AdminBundle in other porjects?
If you notice that you can't use any or all of these bundles for other project, maybe this separations isn't good.
Why I say that?
Because if they can't be used in separated ways is likely that they should be used togheter so you should keep them togheter.
So I advice to keep them into a single bundle (YourNameYourBundleNameBundle (in that notation, for example)) and to separate the single "areas":
FrontEnd controllers/entities/views
Custom controllers/entities/views
Admin controllers/entities/views
This problem has been discussed at length already. For application specific code either do a single AppBundle or don't use bundles at all.

Symfony - What is required to create a plugin?

I've been going through the Symfony documentation in order to find out how to create a plugin. However, the two tutorials seem to give a lot of extra information (for example models etc).
What I'd like to know is, what is the absolute minimum requirement in order to get a controller and template working from a plugin directory?
For example, just an index action and a corresponding 'Hello World' template.
Also, is the routing for this automatic or do I have to manually change something?
Any advice appreciated.
Thanks.
To do what youre askign you would need the following:
MyPlugin/
modules/
my_module/
actions/
actions.class.php
templates/
indexSuccess.php
You would then need to enable the plugin in you ProjectConfiguration and also enable the module in your settings.yml for any apps you want to use it.
Routing is not automatic. You need to add routes manually to routing.yml or you can create a listener and appends/prepends the routes when routing.load_configuration is fired. USing the second option would also imply creating a PluginConfiguration class where you listeners connect to the event via the event dispatcher.
Basically a Plugin follows the same basic structure as an application - except pretty much everything is optional. Whether or not you need to do somethign really depends on what your plugin does. Also you might want to take a look at using sfTaskExtraPlugin it has a task for generating a basic plugin skeleton and a plugin module skeleton.
some examples
enable the plugin in you ProjectConfiguration
go to 'core\config\ProjectConfiguration.class.php' and add next code in setup()
$this->enablePlugins('MyPlugin');
enable the module in your settings.yml
all:
.settings:
enabled_modules: [my_module]

Categories