Remove body PSR-7 Slim3 middleware?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm trying to redirect user to a login page when is not authenticated.
I'm using a middleware in Slim3 to check using Sentinel.
Works but I need to override the body to not show the content. For example, I could use CURL to access to a route like /users and I can get all the page. Because of that I need to remove/override the body if the user is not authenticated.



public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{

$route = parse_url($request->getUri(), PHP_URL_PATH);

if ($route !== '/login' && ! $user = Sentinel::check() )
{
$response = $response
->withStatus(301)
->withHeader("location", '/login')
;
}
return $next($request, $response);

}









share|improve this question


















  • 1





    When not authenticated, I wouldn't think that the middleware stack would even let the request proceed to any content generation. I think you could do that here by simply returning $response->with... instead of letting it proceed to the $next call.

    – Greg Schmidt
    Nov 17 '18 at 1:21











  • You are right. Also, the body will be restored if I call next. So I cannot change it and call $next.

    – Felipe Morales
    Nov 17 '18 at 1:29








  • 1





    Well, you could capture the return value from $next, then override the body, then return as required. That's the expected way of modifying something "on the way out", for example recognizing URLs in the body and replacing them with links. Returning early seems by far the better approach for your need, for a number of reasons; just wanted to be clear that it is absolutely possible to modify the response that the next thing in the middleware queue passes back to you.

    – Greg Schmidt
    Nov 17 '18 at 1:33






  • 1





    Is ! $user = Sentinel::check() correct?

    – Script47
    Nov 17 '18 at 1:37













  • yes that works fine, the library(sentinel) returns false if the user is not authenticated

    – Felipe Morales
    Nov 17 '18 at 1:50


















0















I'm trying to redirect user to a login page when is not authenticated.
I'm using a middleware in Slim3 to check using Sentinel.
Works but I need to override the body to not show the content. For example, I could use CURL to access to a route like /users and I can get all the page. Because of that I need to remove/override the body if the user is not authenticated.



public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{

$route = parse_url($request->getUri(), PHP_URL_PATH);

if ($route !== '/login' && ! $user = Sentinel::check() )
{
$response = $response
->withStatus(301)
->withHeader("location", '/login')
;
}
return $next($request, $response);

}









share|improve this question


















  • 1





    When not authenticated, I wouldn't think that the middleware stack would even let the request proceed to any content generation. I think you could do that here by simply returning $response->with... instead of letting it proceed to the $next call.

    – Greg Schmidt
    Nov 17 '18 at 1:21











  • You are right. Also, the body will be restored if I call next. So I cannot change it and call $next.

    – Felipe Morales
    Nov 17 '18 at 1:29








  • 1





    Well, you could capture the return value from $next, then override the body, then return as required. That's the expected way of modifying something "on the way out", for example recognizing URLs in the body and replacing them with links. Returning early seems by far the better approach for your need, for a number of reasons; just wanted to be clear that it is absolutely possible to modify the response that the next thing in the middleware queue passes back to you.

    – Greg Schmidt
    Nov 17 '18 at 1:33






  • 1





    Is ! $user = Sentinel::check() correct?

    – Script47
    Nov 17 '18 at 1:37













  • yes that works fine, the library(sentinel) returns false if the user is not authenticated

    – Felipe Morales
    Nov 17 '18 at 1:50














0












0








0








I'm trying to redirect user to a login page when is not authenticated.
I'm using a middleware in Slim3 to check using Sentinel.
Works but I need to override the body to not show the content. For example, I could use CURL to access to a route like /users and I can get all the page. Because of that I need to remove/override the body if the user is not authenticated.



public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{

$route = parse_url($request->getUri(), PHP_URL_PATH);

if ($route !== '/login' && ! $user = Sentinel::check() )
{
$response = $response
->withStatus(301)
->withHeader("location", '/login')
;
}
return $next($request, $response);

}









share|improve this question














I'm trying to redirect user to a login page when is not authenticated.
I'm using a middleware in Slim3 to check using Sentinel.
Works but I need to override the body to not show the content. For example, I could use CURL to access to a route like /users and I can get all the page. Because of that I need to remove/override the body if the user is not authenticated.



public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{

$route = parse_url($request->getUri(), PHP_URL_PATH);

if ($route !== '/login' && ! $user = Sentinel::check() )
{
$response = $response
->withStatus(301)
->withHeader("location", '/login')
;
}
return $next($request, $response);

}






php slim-3 psr-7






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 '18 at 1:07









Felipe MoralesFelipe Morales

72511230




72511230








  • 1





    When not authenticated, I wouldn't think that the middleware stack would even let the request proceed to any content generation. I think you could do that here by simply returning $response->with... instead of letting it proceed to the $next call.

    – Greg Schmidt
    Nov 17 '18 at 1:21











  • You are right. Also, the body will be restored if I call next. So I cannot change it and call $next.

    – Felipe Morales
    Nov 17 '18 at 1:29








  • 1





    Well, you could capture the return value from $next, then override the body, then return as required. That's the expected way of modifying something "on the way out", for example recognizing URLs in the body and replacing them with links. Returning early seems by far the better approach for your need, for a number of reasons; just wanted to be clear that it is absolutely possible to modify the response that the next thing in the middleware queue passes back to you.

    – Greg Schmidt
    Nov 17 '18 at 1:33






  • 1





    Is ! $user = Sentinel::check() correct?

    – Script47
    Nov 17 '18 at 1:37













  • yes that works fine, the library(sentinel) returns false if the user is not authenticated

    – Felipe Morales
    Nov 17 '18 at 1:50














  • 1





    When not authenticated, I wouldn't think that the middleware stack would even let the request proceed to any content generation. I think you could do that here by simply returning $response->with... instead of letting it proceed to the $next call.

    – Greg Schmidt
    Nov 17 '18 at 1:21











  • You are right. Also, the body will be restored if I call next. So I cannot change it and call $next.

    – Felipe Morales
    Nov 17 '18 at 1:29








  • 1





    Well, you could capture the return value from $next, then override the body, then return as required. That's the expected way of modifying something "on the way out", for example recognizing URLs in the body and replacing them with links. Returning early seems by far the better approach for your need, for a number of reasons; just wanted to be clear that it is absolutely possible to modify the response that the next thing in the middleware queue passes back to you.

    – Greg Schmidt
    Nov 17 '18 at 1:33






  • 1





    Is ! $user = Sentinel::check() correct?

    – Script47
    Nov 17 '18 at 1:37













  • yes that works fine, the library(sentinel) returns false if the user is not authenticated

    – Felipe Morales
    Nov 17 '18 at 1:50








1




1





When not authenticated, I wouldn't think that the middleware stack would even let the request proceed to any content generation. I think you could do that here by simply returning $response->with... instead of letting it proceed to the $next call.

– Greg Schmidt
Nov 17 '18 at 1:21





When not authenticated, I wouldn't think that the middleware stack would even let the request proceed to any content generation. I think you could do that here by simply returning $response->with... instead of letting it proceed to the $next call.

– Greg Schmidt
Nov 17 '18 at 1:21













You are right. Also, the body will be restored if I call next. So I cannot change it and call $next.

– Felipe Morales
Nov 17 '18 at 1:29







You are right. Also, the body will be restored if I call next. So I cannot change it and call $next.

– Felipe Morales
Nov 17 '18 at 1:29






1




1





Well, you could capture the return value from $next, then override the body, then return as required. That's the expected way of modifying something "on the way out", for example recognizing URLs in the body and replacing them with links. Returning early seems by far the better approach for your need, for a number of reasons; just wanted to be clear that it is absolutely possible to modify the response that the next thing in the middleware queue passes back to you.

– Greg Schmidt
Nov 17 '18 at 1:33





Well, you could capture the return value from $next, then override the body, then return as required. That's the expected way of modifying something "on the way out", for example recognizing URLs in the body and replacing them with links. Returning early seems by far the better approach for your need, for a number of reasons; just wanted to be clear that it is absolutely possible to modify the response that the next thing in the middleware queue passes back to you.

– Greg Schmidt
Nov 17 '18 at 1:33




1




1





Is ! $user = Sentinel::check() correct?

– Script47
Nov 17 '18 at 1:37







Is ! $user = Sentinel::check() correct?

– Script47
Nov 17 '18 at 1:37















yes that works fine, the library(sentinel) returns false if the user is not authenticated

– Felipe Morales
Nov 17 '18 at 1:50





yes that works fine, the library(sentinel) returns false if the user is not authenticated

– Felipe Morales
Nov 17 '18 at 1:50












1 Answer
1






active

oldest

votes


















1














You should not call the $next callback if you only want to redirect the user:



public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
$route = parse_url($request->getUri(), PHP_URL_PATH);

if ($route !== '/login' && ! $user = Sentinel::check() )
{
return $response
->withHeader('Location', '/login')
->withStatus(302);
}

return $next($request, $response);
}





share|improve this answer


























  • While this code works, withRedirect() method is not part of ResponseInterface but SlimHttpResponse.

    – Zamrony P. Juhara
    Nov 18 '18 at 22:35












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347273%2fremove-body-psr-7-slim3-middleware%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You should not call the $next callback if you only want to redirect the user:



public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
$route = parse_url($request->getUri(), PHP_URL_PATH);

if ($route !== '/login' && ! $user = Sentinel::check() )
{
return $response
->withHeader('Location', '/login')
->withStatus(302);
}

return $next($request, $response);
}





share|improve this answer


























  • While this code works, withRedirect() method is not part of ResponseInterface but SlimHttpResponse.

    – Zamrony P. Juhara
    Nov 18 '18 at 22:35
















1














You should not call the $next callback if you only want to redirect the user:



public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
$route = parse_url($request->getUri(), PHP_URL_PATH);

if ($route !== '/login' && ! $user = Sentinel::check() )
{
return $response
->withHeader('Location', '/login')
->withStatus(302);
}

return $next($request, $response);
}





share|improve this answer


























  • While this code works, withRedirect() method is not part of ResponseInterface but SlimHttpResponse.

    – Zamrony P. Juhara
    Nov 18 '18 at 22:35














1












1








1







You should not call the $next callback if you only want to redirect the user:



public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
$route = parse_url($request->getUri(), PHP_URL_PATH);

if ($route !== '/login' && ! $user = Sentinel::check() )
{
return $response
->withHeader('Location', '/login')
->withStatus(302);
}

return $next($request, $response);
}





share|improve this answer















You should not call the $next callback if you only want to redirect the user:



public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
$route = parse_url($request->getUri(), PHP_URL_PATH);

if ($route !== '/login' && ! $user = Sentinel::check() )
{
return $response
->withHeader('Location', '/login')
->withStatus(302);
}

return $next($request, $response);
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 10:43

























answered Nov 17 '18 at 14:04









odanodan

2,18811022




2,18811022













  • While this code works, withRedirect() method is not part of ResponseInterface but SlimHttpResponse.

    – Zamrony P. Juhara
    Nov 18 '18 at 22:35



















  • While this code works, withRedirect() method is not part of ResponseInterface but SlimHttpResponse.

    – Zamrony P. Juhara
    Nov 18 '18 at 22:35

















While this code works, withRedirect() method is not part of ResponseInterface but SlimHttpResponse.

– Zamrony P. Juhara
Nov 18 '18 at 22:35





While this code works, withRedirect() method is not part of ResponseInterface but SlimHttpResponse.

– Zamrony P. Juhara
Nov 18 '18 at 22:35




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347273%2fremove-body-psr-7-slim3-middleware%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python