Is it possible to get session variables and call API in Laravel view composer?
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
add a comment |
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
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 thebootfunction 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
add a comment |
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
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
laravel api session request composer-php
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 thebootfunction 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
add a comment |
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 thebootfunction 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
bootfunction 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