New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slim 3 access response/request object from another class #1750

Closed
pokakhoaitay opened this Issue Feb 3, 2016 · 8 comments

Comments

Projects
None yet
7 participants
@pokakhoaitay

pokakhoaitay commented Feb 3, 2016

I have a Utils class, I need to use $response/$request from Slim 3 to use some useful methods (ex: $request->getUri()->getPath(), etc.), how can I access to $request or $response like this or any solution for me to write a helper class like this:

class Utils
{

    public static function AllowRoutePass($request, $response)
    {
        //How can i access $request, $response here
        $reqMethod =$request->getUri()->getPath();
        if (in_array($reqMethod, AppConfig::IGNORE_ROUTES))
            return true;
        return false;
    }

}

@tuupola

This comment has been minimized.

Show comment
Hide comment
@tuupola

tuupola Feb 3, 2016

Contributor

What you describe sounds like a good candidate for middleware.

Contributor

tuupola commented Feb 3, 2016

What you describe sounds like a good candidate for middleware.

@pokakhoaitay

This comment has been minimized.

Show comment
Hide comment
@pokakhoaitay

pokakhoaitay Feb 3, 2016

A also create a middleware (referrence to this https://github.com/akrabat/slim3-skeleton/blob/master/app/dependencies.php ):

  • Index.php

    $container = $app->getContainer();
    $container['xcore\AppCore']=function ($c){
    return new xcore\AppCore($c);
    };
    
  • Utils.php

    use Psr\Http\Message\ServerRequestInterface as Request;
    use Psr\Http\Message\ResponseInterface as Response;
    final class AppCore
    {
    function __construct($c)
    {
    }
    public function AllowRoutePass(Request $request, Response $response)
    {
        $reqMethod =$request->getUri()->getPath();
        if (in_array($reqMethod, AppConfig::IGNORE_ROUTES))
            return true;
        return false;
    }
    
    

}

But i still have error and unable access $request/$response object.

Anyway, any solution for me that access $request/$response object without create a middleware?

Thanks!

pokakhoaitay commented Feb 3, 2016

A also create a middleware (referrence to this https://github.com/akrabat/slim3-skeleton/blob/master/app/dependencies.php ):

  • Index.php

    $container = $app->getContainer();
    $container['xcore\AppCore']=function ($c){
    return new xcore\AppCore($c);
    };
    
  • Utils.php

    use Psr\Http\Message\ServerRequestInterface as Request;
    use Psr\Http\Message\ResponseInterface as Response;
    final class AppCore
    {
    function __construct($c)
    {
    }
    public function AllowRoutePass(Request $request, Response $response)
    {
        $reqMethod =$request->getUri()->getPath();
        if (in_array($reqMethod, AppConfig::IGNORE_ROUTES))
            return true;
        return false;
    }
    
    

}

But i still have error and unable access $request/$response object.

Anyway, any solution for me that access $request/$response object without create a middleware?

Thanks!

@akrabat

This comment has been minimized.

Show comment
Hide comment
@akrabat

akrabat Feb 3, 2016

Member

I don't understand the problem. The $request object is a parameter to the AllowRoutePass method, so pass it in when you call that method.

Please show the code used to call AllowRoutePass.

Member

akrabat commented Feb 3, 2016

I don't understand the problem. The $request object is a parameter to the AllowRoutePass method, so pass it in when you call that method.

Please show the code used to call AllowRoutePass.

@pokakhoaitay

This comment has been minimized.

Show comment
Hide comment
@pokakhoaitay

pokakhoaitay Feb 3, 2016

I have a middleware, similar to this:

<?php
class ExampleMiddleware
{
   public function __invoke($request, $response, $next)
    {
        //I want to use some methods from my own Helper class (maybe used in other projects) at here. How can i use $request, $response object in my class?
        //Ex: MyHelper->CheckSomethingWhichNeedRequestAndResponseObject();
        //I don't want to pass parameters to my helper methods like this: 
        //Ex: MyHelper->CheckSomethingWhichNeedRequestAndResponseObject($request, $response);
        $response = $next($request, $response);
        $response->getBody()->write('AFTER');
        return $response;
    }
}

My problem is how can i re-use my helper class but still can access $request, $response from Slim 3 framework. In other word, I want MyHelper class able to use any where-any time.
Please give me advice!
Thank you!

pokakhoaitay commented Feb 3, 2016

I have a middleware, similar to this:

<?php
class ExampleMiddleware
{
   public function __invoke($request, $response, $next)
    {
        //I want to use some methods from my own Helper class (maybe used in other projects) at here. How can i use $request, $response object in my class?
        //Ex: MyHelper->CheckSomethingWhichNeedRequestAndResponseObject();
        //I don't want to pass parameters to my helper methods like this: 
        //Ex: MyHelper->CheckSomethingWhichNeedRequestAndResponseObject($request, $response);
        $response = $next($request, $response);
        $response->getBody()->write('AFTER');
        return $response;
    }
}

My problem is how can i re-use my helper class but still can access $request, $response from Slim 3 framework. In other word, I want MyHelper class able to use any where-any time.
Please give me advice!
Thank you!

@mbretter

This comment has been minimized.

Show comment
Hide comment
@mbretter

mbretter Feb 3, 2016

in your code snippet you are passing the container to the constructor of your AppCore class:

final class AppCore
{

function __construct($c)
{
    $this->request = $c->request;
}

your pass directly the request object to your constructor:

$container = $app->getContainer();
$container['xcore\AppCore']=function ($c){
return new xcore\AppCore($c->request);
};

function __construct($request)
{

mbretter commented Feb 3, 2016

in your code snippet you are passing the container to the constructor of your AppCore class:

final class AppCore
{

function __construct($c)
{
    $this->request = $c->request;
}

your pass directly the request object to your constructor:

$container = $app->getContainer();
$container['xcore\AppCore']=function ($c){
return new xcore\AppCore($c->request);
};

function __construct($request)
{
@pokakhoaitay

This comment has been minimized.

Show comment
Hide comment
@pokakhoaitay

pokakhoaitay Feb 3, 2016

oh, you mean i should create a $request variable in my class and assign to it from the consrtuctor. and the methods in my class will read the $request variable. Sound like my class now not a 'helper' class, it's a 'dependency'. Because I want to make my class look like 'portable', it should be have not any variable inside, even i want the methods in my class are static, that mean i no need create an instance whenever i use my class. hmm...maybe i can't do that in Slim!!?

Thank everyone, I have workaround by implemented my class like this: https://github.com/brtriver/slim3-controller/blob/master/src/Controller/Controller.php, like @mbretter suggested and more understand about @akrabat's slim-skeleton project. It working now. But it is not my intention as my describe above. Perhaps i temporary implement my class like that first.

pokakhoaitay commented Feb 3, 2016

oh, you mean i should create a $request variable in my class and assign to it from the consrtuctor. and the methods in my class will read the $request variable. Sound like my class now not a 'helper' class, it's a 'dependency'. Because I want to make my class look like 'portable', it should be have not any variable inside, even i want the methods in my class are static, that mean i no need create an instance whenever i use my class. hmm...maybe i can't do that in Slim!!?

Thank everyone, I have workaround by implemented my class like this: https://github.com/brtriver/slim3-controller/blob/master/src/Controller/Controller.php, like @mbretter suggested and more understand about @akrabat's slim-skeleton project. It working now. But it is not my intention as my describe above. Perhaps i temporary implement my class like that first.

@geggleto

This comment has been minimized.

Show comment
Hide comment
@geggleto

geggleto Feb 5, 2016

Contributor

@mbretter You should never get the request from the container. ever. The Request is set in the container once, and once middleware is applied is no longer the valid current request.

Contributor

geggleto commented Feb 5, 2016

@mbretter You should never get the request from the container. ever. The Request is set in the container once, and once middleware is applied is no longer the valid current request.

@clemens-tolboom

This comment has been minimized.

Show comment
Hide comment
@clemens-tolboom

clemens-tolboom Sep 12, 2018

You should never get the request from the container. ever. The Request is set in the container once, and once middleware is applied is no longer the valid current request.

@geggleto the documentation has no warning for this. Can we improve this somehow?

I checked here https://www.slimframework.com/docs/v3/concepts/di.html#required-services

I need the applications basepath and both request and router provides this. Guess the latter is best.

$router = $app->getContainer()->router

[edit]But that has not getter for basePath :-([/edit]

clemens-tolboom commented Sep 12, 2018

You should never get the request from the container. ever. The Request is set in the container once, and once middleware is applied is no longer the valid current request.

@geggleto the documentation has no warning for this. Can we improve this somehow?

I checked here https://www.slimframework.com/docs/v3/concepts/di.html#required-services

I need the applications basepath and both request and router provides this. Guess the latter is best.

$router = $app->getContainer()->router

[edit]But that has not getter for basePath :-([/edit]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment