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

$app->group scope?

troy's Avatar

troy

24 Sep, 2015 03:53 PM

I am new to Slim. Loving it so far. I am building an application that is mostly an API that serves JSON-formatted data. Pretty standard stuff. I am serving HTML docs, too.

I have a URL pattern where http://myapp.com/api/* means it is an API request.

So I used $app->group as you'll see in the code snippet below. The idea is that all the URL methods contained in that group are API methods and will start with "/api".

Of course I shortened the example below to keep it minimal--just enough to clarify the problem. I noticed that in all my methods, I was adding the CORS header and setting the ContentType to JSON. As you do when writing code, I saw a refactoring opportunity. So I figured I would just extract those 2 lines and put them in the group to apply to all methods in the group as you'll see below.

It worked! Yeah! ...but it also made those changes "globally". That is, all my URLs--whether defined in this route file or elsewhere--now have ContentType set to JSON, which of course breaks my HTML doc serving.

What is the best Slim way to extract these bits to apply only to the URLs in the group?

<?php

$app->group('/api', $authenticate($app), function () use ($app) {

    // Grab easy reference to Response object
    $response = $app->response();
    
    // Set CORS Header to allow cross-site access to these AJAX methods 
    $response->header('Access-Control-Allow-Origin', '*');

    // All API calls return JSON-formatted data
    $app->contentType('application/json');
/** 
OOPS!!! That just set ALL app responses to be JSON responses!
I only want to affect URL methods in this group.
*/

    // URL method to return array of all car manufacturers
    $app->get('/makes', function () use ($response) {
        $data = Maker()::all_manufacturers();  // returns array of makers
        $response->write(json_encode($data));

    });

    // URL method to return array of all models for a specific manufacturer
    $app->get('/makes/:maker_name/models', function ($maker_name) use ($response) {
        $maker = new Maker($maker_name);
        $data = $maker->models();  // returns array of models
        $response->write(json_encode($data));
    });

});
  1. 1 Posted by Florian on 16 Oct, 2015 10:23 AM

    Florian's Avatar

    Hi,

    I totally agree, I have the same problem here. All the code in my groups are being called.

    This is because call_user_func is called in Slim2/Slim.php around line 550

    I'm trying to fix it, what you can do is just check between the URI and the group pattern :

    Slim.php around line 550 :

    if($this->request->getPath() contains $pattern){
        call_user_func
    }
    

    and it seems to be working !

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