Language routing in Silex - php

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.

Related

Laravel Route Controller

I have a working solution within routes.php, but I understand that laravel can handle restful routes better. I've tried using their documentation to implement restful resource controllers, but had no luck.
This is what I have at the moment
Route::get('/invoices', 'InvoicesController#showInvoices');
Route::get('/invoices/data', 'InvoicesController#getInvoices');
Basically, the showInvoices returns the invoices view and getInvoices returns a JSON string for DataTables which is called from the invoices view.
So I want to be able to call /invoices to get the view and then call /invoices/data using JavaScript.
Any suggestions how to convert this to a resource controller or more suitable controller?
Yes, there was a cleaner way. Route controllers were supported up to Laravel 5.3. Then this functionality was removed in favor of explicit routes, which leave the routes files in disarray when you have lots of routes.
Fortunately, there is a class I wrote called AdvancedRoute, which serves as a drop in replacement.
In your case you can use it like this:
Route::get('/invoices', 'InvoicesController#showInvoices');
Route::get('/invoices/data', 'InvoicesController#getInvoices');
Becomes:
AdvancedRoute::controller('/invoices', 'InvoicesController');
Explicit routes are built automatically for you. Have in mind you have to follow a convention by prefixing the method names with the request method, which I personally find very clean and developer friendly:
InvoicesController#getInvoices => /invoices
InvoicesController#getInvoicesData => /invoices/data
Full information how to install and use find at the GitHub repo at:
https://github.com/lesichkovm/laravel-advanced-route
Hope you find this useful.
You could create a "resource" route like so:
Route::resource('/invoices', 'InvoicesController');
Which will provide RESTful routes (GET, POST, PUT, etc...) for that particular /invoices route/resource. You can check this by executing php artisan route:list
You can learn more here.
I hope this helped.
Cheers!

How to implements dynamic routes in Symfony2?

we have a CMS system build on top of symfony2 and I've been struggling with routing problem, when you would like to implement behavior similiar to every CMS system using friendly URL slug as identifier of entity.
Let's say I have multiple bundles, each of them taking care of their stuff and entities. How can I use their own controllers with dynamic route param slug?
For example, I have a base slug controller with route "/{_slug}/" with lowest priority. So it can found entity by slug in repositories which knows about, but this solution is not flexible. And also it kind of degrading controllers, because now you have only one master controller, instead of deffering the logic to each controller of each bundle.
I found several ideas of approach to this problem.
Load routes from database - a little worse performance, no cached routes
Add dynamic loader of routes - Too much code, worse control of slugs
Custom router, which will be used before symfony core CMF router - so far I've found a little information about this solution
I've found several topic, that tries to cover this problem:
More complex routing - Discussion about dnyamic routes
How to add custom routes to Symfony 2 before container compilation?
If you haven't already, I suggest to take a look at the "Dynamic Router" from the RoutingBundle included in the Symfony CMF project.
Since the CMF project is about building CMS functionalities on top of Symfony, I think it may fit your needs.

CakePHP restful API routing to a set of controllers

I'm wondering if it's possible to set a route in Cakephp that will allow me to route to a set of controllers where a prefix is set. What I'm looking to do is duplicate most of my controllers but for a REST api.
I'd like to still have all the default controllers for the site itself but have a product controller for instance that only returns JSON. For this I would like to move all api controllers into a subfolder and route to them by detecting /api/controllername.
Is this possible or even the correct way to accomplish this?
What I'm looking to do is duplicate most of my controllers but for a
REST api.
Bad idea. IMHO. You're going to duplicate code very likely and scatter code everywhere. Keep it in one place. This sentence is an indicator that makes me think you have to much logic in your controllers. Move it to models, fat models.
There are several better ways to do this.
Use a prefix for routing to API methods inside your related controllers (api/v1/foo/bar -> ControllerName::api_actionName())
Implement a single API controller that dispatches model methods (api/v1/foo/bar -> FooModel::barMethod($queryParam1, $queryparam2,...)
Implement a service layer that sits between the model and the controllers and implement a dispatcher filter or the API controller from the 2nd suggestion to dispatch the service methods. You'll use services instead of models then. Controller <-> Service <-> Model. To implement this well some experience with the framework and design patterns is required. If its not well done it will probably cause more problems than benefit - IMHO.
If it's a more or less simple API and the API logic is similar to your actions you can simple re-use the same controller actions you already have and just usethe built in REST and JSON/XML view serialization and could still use routing to create a fancy route (api/v1/...) for them. You can then do conditional checks as well if the controller is called as API.
It's up to you which one you pick, I've seen and used them all in action, they all work the difference is mostly the implementation and level of abstraction you need. However, the key point is to write clean and DRY code and care about SoC.

use cakephp component inside a model

How do I use a component that I created in cakePHP inside one of my model classes? Is this possible?
If so, please let me know how I can do so
It is possible but pretty bad practice in a MVC framework. You should re-think and re-organize your code if you think you need to use the component in a model because something is cleary wrong then.
A component is thought to share code between controllers, only between controllers.
Components in CakePHP 1.3
Components in CakePHP 2.x
Components in CakePHP 3.x
To share re-usable code between models it would be a behavior. For a view it would be a helper.
If you have some really generic code it should be a lib or put it in the Utility folder / namespace or create a new namespace. Check the existing classes there to get an idea what to put in there.
No code was provided so it is not possible to give any real recommendation on how to refactor it. However the way you want to use the existing code won't work in the MVC context, so time to rethink your approach of whatever you try to do.
It is possible to use a component inside a model (but I cannot comment if this is a recommended or a best-practice).
Inspired from original source, an example to use a component called ‘Geocoder’ in a model:
App::import('Component','GeoCoder');
$gc = new GeoCoderComponent(new ComponentCollection);
Then you can use $gc to call the functions of the component.
--
P.S.: I do not want to encourage bad programming practices, but sometimes the pressure of deadlines (in real world projects) may force a developer to make such decisions.
#AD7six
// Use anywhere
AuthComponent::user('id')
// From inside a controller
$this->Auth->user('id');
From the cake PHP documentation they provide AuthComponent::user('id') so that it can be used in places other than a controller.
Maybe I need a bigger hint, but why shouldn't my model be able to access ACL information ?

How access to database on routing file with Symfony2?

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

Categories