smile 22121
smile 22121

Reputation: 295

Slim controller function missing argument 1

I have simple Slim app that should render a html page. This is my index.php

require __DIR__ . '/../vendor/autoload.php';
$app = new \RKA\Slim(
    [
        'mode' => 'development',
    ]
);
// Optionally register a controller with the container
$app->container->singleton('App\Home', function ($container) {
    return new \App\Controller\Home();
});
// Set up routes
$app->get('/','App\Home:index');

This is my controller Home.php namespace App\Controller;

class Home
{
    protected $request;
    protected $response;
    public function index($app)
    {
        $this->app = $app;
        $this->app->render('../test.html');
    }

I get Message: Missing argument 1 for App\Controller\Home::index()

Upvotes: 0

Views: 274

Answers (1)

Rafael
Rafael

Reputation: 18522

I see you're not using original Slim, but Slim extended using the RKA Slim Controller project.

As far as I understand the code, your index will not get the app passed as param. The method will get only URL params that the route defined (in this case none).

If you need reference to the app, implement method named setApp, that will be called automatically (when dispatching the route).

class Home
{
    protected $request;
    protected $response;
    protected $app;

    public function setApp($app)
    {
        $this->app = $app;
    }

    public function index() {
        /* logic for the route
           app is available as $this->app */
    }
}

If you additionally implement setRequest and setResponse, you will get the request and response references.

Upvotes: 1

Related Questions