The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Setting Attributes in a Route

alex.stansfield's Avatar

alex.stansfield

07 Jul, 2015 02:57 AM

I'm currently trying to port a project from Symfony 2 across to Slim 3 as it seems much more suited for a small api that needs to perform well.

The service itselfs works as a sort of facade between the outside work and our internal web services. Pretty much all the routes can be served by a single controller and 4 actions (GET, POST, PUT, DELETE). Middleware then did the service specific business logic (auth, body/param/response manipulation).

The problem I'm having is that in symfony I really relied on being able to set attributes in the route configuration. The middleware would then fetch the correct configuration for what it needed to do from these attributes.

I can't seem to see a way to easily do this in Slim 3 route. It seems I might be able to do it with some nightmare regular expressions but I'm pretty sure that would become an unmaintainable mess pretty quickly.

Is there something I'm missing?

  1. Support Staff 1 Posted by Andrew Smith on 07 Jul, 2015 10:09 AM

    Andrew Smith's Avatar

    Hi Alex, could you give me a code example of what you are trying to achieve please?

  2. 2 Posted by alex.stansfield on 08 Jul, 2015 10:00 AM

    alex.stansfield's Avatar

    So for example (please note this is code I just wrote mostly off the top of
    my head). Say we may need to change the the body of a post/put request
    based on which service and action the request will be going to. We have
    this middleware class:

    <?php
    
    namespace Facade\Middleware;
    
    use Slim\Http\Request;
    use Slim\Http\Response;
    use Facade\BodyTranslator\BodyTranslator;
    
    class RequestTranslator
    {
        public function translateBody(Request $request, Response $response, $next)
        {
            $service = $request->getAttribute('service');
            $action = $request->getAttribute('action');
    
            // Get the correct Body translator
            $bodyTranslator = BodyTranslator::factory($service, $action);
    
            // Get the translated body
            $body = $bodyTranslator->translate($request);
    
            // Create the request with the new body
            $request = $request->withParsedBody($body);
    
            return $next($request, $response);
        }
    }
    
    ?>
    

    The $service and $action variables depend on the route, most of the time
    they will match part of the url (which makes things easier) sometimes
    however they wont.

    In symfony2 I could define a route in yaml like this:

    facade_report_put:
        pattern:  /report/{version}/{action}.{format}/{id}
        defaults: { _controller: FacadeBundle:Facade:put, service:
    reporting }
        methods: [PUT]
        requirements:
          version: v1|v2
          format: json|xml
          action: report|chart|config
    

    I could then get service and action from the request attributes. The action
    being defined by the url matching but the service being manually set.

    I was just trying to figure out how, if possible, that could be done in
    slim 3.

    edited to make example class a bit simpler

  3. 3 Posted by alex.stansfield on 08 Jul, 2015 04:56 PM

    alex.stansfield's Avatar

    Added ability to set default attributes on a route.

    Pull Request here: https://github.com/slimphp/Slim/pull/1343

  4. 4 Posted by jenoYAdiz on 04 Feb, 2016 10:33 AM

    jenoYAdiz's Avatar

    Hello, Community!

    The Company - PerfectVPN . Com

    Anonymous Internet Surfing, Anonymous File Sharing Torrent-P2P, Amonymous WiFI.
    Fully Automatic and Anonymous VPN Service NO Logs , You don’t have to waste time on:

    1 - Access to all servers for just 9 EURO.
    DoubleVPN, OPENVPN and PPTP / L2TP / IpSec VPN - Access to 40 Servers in 29 Countries!
    Subscribe to All Servers of Our Service for 9 Euro Month; 3 months = 20 Euro; 6 months = 35 Euro; 1 year = 55 Euro.
    Types of VPN connections included in a single subscription - DoubleVPN and OpenVPN.
    The Speed Of All Our Servers OpenVPN = 1000 M.Bit / Unlimited Traffic !!!

    2 - Unique “VPN Client” is easy to install on All Kinds of Operating Systems: MAC / Windows / Linux / Android !!!
    Will allow you to switch easily between the VPN servers in:

    Will allow you to switch easily between the VPN Servers in:

    USA
    Germany
    Canada
    Austria
    Czech Republic
    Denmark
    Egypt
    Hong Kong
    Iceland
    India
    Italy
    Latvia
    Luxembourg
    Malaysia
    Netherlands
    Norway
    Panama
    Romania
    North Africa
    Spain
    Switzerland
    Sweden
    England
    Vietnam
    Australia

    In the near future our service will be available in Spain, Greece, Sweden, Mexico, Czech Republic, Poland, China, Belgium ...

    3 - The program features an easy configuration of a VPN connection to your Internet:
    - Automatically Block Internet Connection when Disconnected from a VPN. - Automatic connection to the VPN when you turn on the Internet.

    4 - Also,our Service has a unique N-tier, Partnership Program:
    You will get 30% of total payments of your customers!

    5 - Company Takes All Kinds Of Payments in Full Auto Mode!
    WebMoney / Visa / Master Card / PayPal / Perfect Money / BitCoin / SMS and many others.

    Our Anonymous VPN Service Is Here!

    https://bitcointalk.org/index.php?topic=143537.0

    PerfectVPN . Com

    Disguise Your Internet Connect!

    ICQ : 6850058
    Skype : In-Disguise-VPN

    Sincerely, Your Anonymous VPN Service : PerfectVPN . Com

  5. 5 Posted by HornyYAdiz on 23 Feb, 2016 10:36 PM

    HornyYAdiz's Avatar

    Hello, Community!

    The Company - PerfectVPN . Com

    Anonymous Internet Surfing, Anonymous File Sharing Torrent-P2P, Amonymous WiFI.
    Fully Automatic and Anonymous VPN Service NO Logs , You don’t have to waste time on:

    1 - Access to all servers for just 9 EURO.
    DoubleVPN, OPENVPN and PPTP / L2TP / IpSec VPN - Access to 40 Servers in 29 Countries!
    Subscribe to All Servers of Our Service for 9 Euro Month; 3 months = 20 Euro; 6 months = 35 Euro; 1 year = 55 Euro.
    Types of VPN connections included in a single subscription - DoubleVPN and OpenVPN.
    The Speed Of All Our Servers OpenVPN = 1000 M.Bit / Unlimited Traffic !!!

    2 - Unique “VPN Client” is easy to install on All Kinds of Operating Systems: MAC / Windows / Linux / Android !!!
    Will allow you to switch easily between the VPN servers in:

    Will allow you to switch easily between the VPN Servers in:

    USA
    Germany
    Canada
    Austria
    Czech Republic
    Denmark
    Egypt
    Hong Kong
    Iceland
    India
    Italy
    Latvia
    Luxembourg
    Malaysia
    Netherlands
    Norway
    Panama
    Romania
    North Africa
    Spain
    Switzerland
    Sweden
    England
    Vietnam
    Australia

    In the near future our service will be available in Spain, Greece, Sweden, Mexico, Czech Republic, Poland, China, Belgium ...

    3 - The program features an easy configuration of a VPN connection to your Internet:
    - Automatically Block Internet Connection when Disconnected from a VPN. - Automatic connection to the VPN when you turn on the Internet.

    4 - Also,our Service has a unique N-tier, Partnership Program:
    You will get 30% of total payments of your customers!

    5 - Company Takes All Kinds Of Payments in Full Auto Mode!
    WebMoney / Visa / Master Card / PayPal / Perfect Money / BitCoin / SMS and many others.

    Our Anonymous VPN Service Is Here!

    https://bitcointalk.org/index.php?topic=143537.0

    PerfectVPN . Com

    Disguise Your Internet Connect!

    ICQ : 6850058
    Skype : In-Disguise-VPN

    Sincerely, Your Anonymous VPN Service : PerfectVPN . Com

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac