Is it possible to get session variables and call API in Laravel view composer?












0















In AppServiceProvider.php, I am trying to get data from session then call API with it then pass a variable after getting it from the response.



Also, I don't know if it's right. I added "Request $request" to boot function as in other parts of code.



And the error I'm getting is "RuntimeException in Request.php line 388: Session store not set on request." Does that mean session variable isn't set? I would've thought they'd be available after I log in to my site as I session put "token" and "member_id" during login.



Is it because view controller is higher level so my session puts during login won't come before bootstrap code in boot function here uses them? Oh, or is the request not really passed in as parameter of boot function as I would've liked it to. How would I otherwise do that or get variables from the session?



Anyway, are the steps I'm taking proper? If I'm doing things incorrectly throughout, such as bad practice, please point it out as well thanks.



Here's my code:



<?php

namespace AppProviders;

use IlluminateSupportServiceProvider;
use IlluminateHttpRequest;

class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(Request $request)
{
$client = new GuzzleHttpClient();

$params = array(
'token' => $request->session()->get('token'),
'member_id' => $request->session()->get('member_id'),
'activity' => 'GET MEMBER INFO'
);

$response = $client->request('POST',
env('SPACE_4_CAR_API_DOMAIN') . 'select_api/GetMemberInfo.php',
['json' => $params]
);

$returnData = json_decode($response->getBody());

view()->composer('layout', function ($view) {
$view->with('is_admin', $returnData->is_administrator);
});
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}









share|improve this question

























  • Laravel session is initialized in a middleware so you can't access the session from a Service Provider, because they execute before the middleware in the request lifecycle. Also the boot function does not have any parameter, so your request is most probably null there.

    – nakov
    Jan 2 at 10:36













  • @nakov Is there then a way to access the session or store it somewhere earlier in request lifecycle?

    – Ben Kao
    Jan 3 at 2:11











  • You can create a custom middleware, and use the session there.

    – nakov
    Jan 3 at 8:46
















0















In AppServiceProvider.php, I am trying to get data from session then call API with it then pass a variable after getting it from the response.



Also, I don't know if it's right. I added "Request $request" to boot function as in other parts of code.



And the error I'm getting is "RuntimeException in Request.php line 388: Session store not set on request." Does that mean session variable isn't set? I would've thought they'd be available after I log in to my site as I session put "token" and "member_id" during login.



Is it because view controller is higher level so my session puts during login won't come before bootstrap code in boot function here uses them? Oh, or is the request not really passed in as parameter of boot function as I would've liked it to. How would I otherwise do that or get variables from the session?



Anyway, are the steps I'm taking proper? If I'm doing things incorrectly throughout, such as bad practice, please point it out as well thanks.



Here's my code:



<?php

namespace AppProviders;

use IlluminateSupportServiceProvider;
use IlluminateHttpRequest;

class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(Request $request)
{
$client = new GuzzleHttpClient();

$params = array(
'token' => $request->session()->get('token'),
'member_id' => $request->session()->get('member_id'),
'activity' => 'GET MEMBER INFO'
);

$response = $client->request('POST',
env('SPACE_4_CAR_API_DOMAIN') . 'select_api/GetMemberInfo.php',
['json' => $params]
);

$returnData = json_decode($response->getBody());

view()->composer('layout', function ($view) {
$view->with('is_admin', $returnData->is_administrator);
});
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}









share|improve this question

























  • Laravel session is initialized in a middleware so you can't access the session from a Service Provider, because they execute before the middleware in the request lifecycle. Also the boot function does not have any parameter, so your request is most probably null there.

    – nakov
    Jan 2 at 10:36













  • @nakov Is there then a way to access the session or store it somewhere earlier in request lifecycle?

    – Ben Kao
    Jan 3 at 2:11











  • You can create a custom middleware, and use the session there.

    – nakov
    Jan 3 at 8:46














0












0








0








In AppServiceProvider.php, I am trying to get data from session then call API with it then pass a variable after getting it from the response.



Also, I don't know if it's right. I added "Request $request" to boot function as in other parts of code.



And the error I'm getting is "RuntimeException in Request.php line 388: Session store not set on request." Does that mean session variable isn't set? I would've thought they'd be available after I log in to my site as I session put "token" and "member_id" during login.



Is it because view controller is higher level so my session puts during login won't come before bootstrap code in boot function here uses them? Oh, or is the request not really passed in as parameter of boot function as I would've liked it to. How would I otherwise do that or get variables from the session?



Anyway, are the steps I'm taking proper? If I'm doing things incorrectly throughout, such as bad practice, please point it out as well thanks.



Here's my code:



<?php

namespace AppProviders;

use IlluminateSupportServiceProvider;
use IlluminateHttpRequest;

class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(Request $request)
{
$client = new GuzzleHttpClient();

$params = array(
'token' => $request->session()->get('token'),
'member_id' => $request->session()->get('member_id'),
'activity' => 'GET MEMBER INFO'
);

$response = $client->request('POST',
env('SPACE_4_CAR_API_DOMAIN') . 'select_api/GetMemberInfo.php',
['json' => $params]
);

$returnData = json_decode($response->getBody());

view()->composer('layout', function ($view) {
$view->with('is_admin', $returnData->is_administrator);
});
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}









share|improve this question
















In AppServiceProvider.php, I am trying to get data from session then call API with it then pass a variable after getting it from the response.



Also, I don't know if it's right. I added "Request $request" to boot function as in other parts of code.



And the error I'm getting is "RuntimeException in Request.php line 388: Session store not set on request." Does that mean session variable isn't set? I would've thought they'd be available after I log in to my site as I session put "token" and "member_id" during login.



Is it because view controller is higher level so my session puts during login won't come before bootstrap code in boot function here uses them? Oh, or is the request not really passed in as parameter of boot function as I would've liked it to. How would I otherwise do that or get variables from the session?



Anyway, are the steps I'm taking proper? If I'm doing things incorrectly throughout, such as bad practice, please point it out as well thanks.



Here's my code:



<?php

namespace AppProviders;

use IlluminateSupportServiceProvider;
use IlluminateHttpRequest;

class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(Request $request)
{
$client = new GuzzleHttpClient();

$params = array(
'token' => $request->session()->get('token'),
'member_id' => $request->session()->get('member_id'),
'activity' => 'GET MEMBER INFO'
);

$response = $client->request('POST',
env('SPACE_4_CAR_API_DOMAIN') . 'select_api/GetMemberInfo.php',
['json' => $params]
);

$returnData = json_decode($response->getBody());

view()->composer('layout', function ($view) {
$view->with('is_admin', $returnData->is_administrator);
});
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}






laravel api session request composer-php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 10:23







Ben Kao

















asked Jan 2 at 10:16









Ben KaoBen Kao

902211




902211













  • Laravel session is initialized in a middleware so you can't access the session from a Service Provider, because they execute before the middleware in the request lifecycle. Also the boot function does not have any parameter, so your request is most probably null there.

    – nakov
    Jan 2 at 10:36













  • @nakov Is there then a way to access the session or store it somewhere earlier in request lifecycle?

    – Ben Kao
    Jan 3 at 2:11











  • You can create a custom middleware, and use the session there.

    – nakov
    Jan 3 at 8:46



















  • Laravel session is initialized in a middleware so you can't access the session from a Service Provider, because they execute before the middleware in the request lifecycle. Also the boot function does not have any parameter, so your request is most probably null there.

    – nakov
    Jan 2 at 10:36













  • @nakov Is there then a way to access the session or store it somewhere earlier in request lifecycle?

    – Ben Kao
    Jan 3 at 2:11











  • You can create a custom middleware, and use the session there.

    – nakov
    Jan 3 at 8:46

















Laravel session is initialized in a middleware so you can't access the session from a Service Provider, because they execute before the middleware in the request lifecycle. Also the boot function does not have any parameter, so your request is most probably null there.

– nakov
Jan 2 at 10:36







Laravel session is initialized in a middleware so you can't access the session from a Service Provider, because they execute before the middleware in the request lifecycle. Also the boot function does not have any parameter, so your request is most probably null there.

– nakov
Jan 2 at 10:36















@nakov Is there then a way to access the session or store it somewhere earlier in request lifecycle?

– Ben Kao
Jan 3 at 2:11





@nakov Is there then a way to access the session or store it somewhere earlier in request lifecycle?

– Ben Kao
Jan 3 at 2:11













You can create a custom middleware, and use the session there.

– nakov
Jan 3 at 8:46





You can create a custom middleware, and use the session there.

– nakov
Jan 3 at 8:46












0






active

oldest

votes











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%2f54004522%2fis-it-possible-to-get-session-variables-and-call-api-in-laravel-view-composer%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f54004522%2fis-it-possible-to-get-session-variables-and-call-api-in-laravel-view-composer%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

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'