Symfony allows the developer to build and maintain web applications in a modular way by exposing custom features to application developers through a separate component, as Symfony 2.x did. Symfony 3.0 introduces a new and handy configuration option to achieve this through one of the components, URlHandler .
URlHandler implements a sort of a router interface that creates HTTP requests to a URL. This can be used in order to build a IP Logger URL shortener solution to easily shorten domain names. As part of the configuration, it exposes the following interfaces:
public interface URlHandler { void init() { // Add URL shortening logic in action(inject URLHandler); } protected interface Action<T> { return Action(urlHandler); } protected interface UrlHandlerResponse { string match; UrlHandlerResponse(string url, string func); Response response; } protected abstract class URLHandler { public static URLHandler get(); public static string send(string url); public static bool isHost(); }
When building the configuration, you can pass a URLHandler to the corresponding endpoint of the action interface to build a new URLHandler , or pass a URLHandler to the action interface to use as a static method in a different component.
Dependency injection of the UrlHandler
You can inject it via the dependencies field of the configuration, which becomes the DefaultRouteType property when you create your application:
Factory methods
For the URLHandler , you can use Factory methods to inject an object that implements Action<T> interface, which then does the actual work of converting a URL and returns a response. In this case, the factory method will act as an interface for any service you expose in the application.
Public static async Task<UrlHandler> getUrlHandler() { UrlHandler response = await createUrlHandler(); await response.send(ConfigureSessionFactory()); return response; } public static List<UrlHandler> getAllURLHandler() { return arrayList(Response::class.get()); } public static UrlHandler(UrlHandler handler) { return new UrlHandler(handler); }
The action method is also a Factory method that has a generic return type:
public static async Task<UrlHandler> getUrls() { try { return factoryMethod.getUrlHandler(); } catch (RuntimeException e) { Log.e(TAG, “unable to create urlHandler”); return null; } }
which represents a method that returns an object of type UrlHandler , and exposes a factory method to the application that defines the configuration.
public static async Task<UrlHandler> createUrlHandler() { IEnumerable<UrlHandler> handler = new URLHandler(HandlerFactory.class); await handler.send(ConfigureSessionFactory()); return handler; } public static async Task<UrlHandler> sendUrls(UrlHandler action) { return Task.enter(configuration => { config = ConfigureSessionFactory.get()); // Create a URLHandler, run the URLHandler, add the URL handler to the session, and log the response. logEntries(ConfigureSessionFactory.get(config), session); // Prepare and send the response. response = await handler.send(config.getUrl(“MyUrl”))); // Log the response, log entries, and break the log with an exception. logEntries(config.getUrl(“Error”), session); // Break the log when the server returns an HTTP 401 to show the user what to do next. session.break(); // Make sure the current URL is redirected to the UrlHandler with the new route. router.requestTimeout(urlHandler.getUrls().map(urlHandler.getUrl(“MyUrl”))); // Remaining URLHandler events. foreach (var event in config.getUrls()) { logEntries(config.getUrlHandler(), event.getClass().getName()); } } }
Here we can see that in addition to any static methods that are attached to the API, you can also use action methods as factory methods in your configuration classes.