Closed csrui closed 9 years ago
It might be worth posting your original code incase I can spot something. If the example shown is called by route or panel then Http $http
is injected for you. But if you decided to call that method like:
(new PostController)->showPost($id);
It wouldn’t work, and need to pass the $http
to it. At any point you can get framework components by calling herbert('component')
So you have two options:
(new PostController)->showPost($id, herbert('http'));
Or
public function showPost($id)
{
$http = herbert('http');
$post = Post::find($id);
if(!$post)
….
I have only this route:
$router->post([
'uri' => '/sc/cart',
'uses' => __NAMESPACE__ . '\Controllers\CartController@add'
]);
and then this controller
<?php namespace ShirtCreator\Controllers;
use ShirtCreator\Models\Shirt;
use Herbert\Framework\Http;
class CartController {
public function index()
{
header('Content-Type: application/json');
return json_encode();
}
public function add() {
// $http->get('shirt_id');
$shirt = Shirt::whereId($_POST['shirt_id'])->first();
You've forgot to add Http $http
in the parameters of the function:
public function add() {}
Should be:
public function add(Http $http) {}
thank you
How is the $http available in this example?
When I try I get Notice: Undefined variable: http in ...