Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SCC's avatar
Level 7

Passing API Data back to view

Hi,

I am experimenting with an API (DirectAdmin), I have a view to select a server which then calls a method in the ServerController and runs the following.

$sock->query('/CMD_API_ALL_USER_USAGE');
$result = $sock->fetch_body();
$result = explode("\n", $result);

That does exactly what it is supposed to do and returns multiple rows of data like this example. ( there is a lot, this is just a small sample of one row):

[ 
    "testuser=bandwidth=7.05128000&creator=adminuser&date_created=1582530762&
    default=test.com&email_daily_limit=1000&email_deliveries_outgoing=0&inode=1492 ",
]

What I want to do now is send this data straight back to the view where I can output it in some way. But when I try to send the data in $result back I either get a bad gateway or simply no data at all, that I can tell.

This is just a personal project, I admit I am not great with this kind of thing and it is probably a simple task, just not sure what I am missing.

Any help appreciated

0 likes
7 replies
Sinnbeck's avatar

Can you show the code that isn't working?

1 like
SCC's avatar
Level 7

Well, at this stage the data I need is in $result as an array, so I thought this would work.

return redirect()->action('Admin\ServerController@index', $result);

But nothing and a few variations of this appear to put the data in. I am passing it back through the ServerController as I could not find a workable solution for anything else.

ntraykov's avatar

Have you tried this?

return view('viewname', ['result' => $result]);

When adding a second argument to the action method, these values are added as route params. Do you really need to redirect?

1 like
SCC's avatar
Level 7

So, yes I have other data being passed into the view, so if I call the view directly it will return an error as that initial data is not passed to it.

This works fine on one server that has 3 websites:

return redirect()->action('Admin\ServerController@index', ['result' => $result]);

However, on the other server with 7 sites I get a 502 bad gateway, I suspect there is too much data being passed in the URL doing it that way.

ntraykov's avatar

Plesae explain the whole architecture because I am trying to understand the reason for your idea.

SCC's avatar
Level 7

Sorted it, it was an issue with the way the data from the API was being passed, nothing to do with Laravel.

Please or to participate in this conversation.