Igor Santos de Lima
Igor Santos de Lima

Reputation: 177

Calling Class:method instead of callback function phpslim3

Most examples of phpslim3 usage looks like this

$app->get('/hello/{name}', function ($request, $response, $args) { ... });

But instead of just, writing the code inside of callback brackets, I prefer to create a class with separated functions;

The point is that my code doesn't work. My response is only:

Message: Callable Api\CreateAccountController does not exist File: C:\Users\dedeu\Documents\Work\php\l21-api-slim\vendor\slim\slim\Slim\CallableResolver.php
Line: 90

I already checked this documentation files, but doesn't work too(works, but I'm doing something wrong)

https://www.slimframework.com/docs/v3/objects/router.html#post-route

composer.json

"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/",
        "App\\": "src/",
        "Api\\": "src/Api/"
    }
},
"autoload": {
    "psr-4": {
        "Api\\": "src/Api/"
    }
},

router.php

<?php

use Slim\Http\Request;
use Slim\Http\Response;
use Api\CreateAccountController;

// Routes

// $app->get('/[{name}]', function (Request $request, Response $response, array $args) {
//     // Sample log message
//     $this->logger->info("Slim-Skeleton '/' route");


$app->post('/api/moip/create-account', \Api\CreateAccountController::class . ':create');

dependencies.php

<?php
// DIC configuration

$container = $app->getContainer();

// view renderer
$container['renderer'] = function ($c) {
    $settings = $c->get('settings')['renderer'];
    return new Slim\Views\PhpRenderer($settings['template_path']);
};

// monolog
$container['logger'] = function ($c) {
    $settings = $c->get('settings')['logger'];
    $logger = new Monolog\Logger($settings['name']);
    $logger->pushProcessor(new Monolog\Processor\UidProcessor());
    $logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], $settings['level']));
    return $logger;
};


$container['CreateAccountController'] = function($c) {
    $view = $c->get("view"); // retrieve the 'view' from the container
    return new \Api\CreateAccountController($view);
};

CreateAccountController.php

<?php

namespace Api;

class CreateAccountController
{

    protected $view;

    public function __construct(\Slim\Views\Twig $view) {
        $this->view = $view;
    }

    public function create($request, $response, $args) {

        $data = $request->getParsedBody();
        $status = $response->getStatusCode();

        if(!isset($data["teste"])){
            // print_r('rara rasputin');
            $out = array('message' => 'you lack infos', 'code' => 400);
            $response = $response->withJson($out, 400);
            return $response;
        }

        $response = $response->withJson($data, 201);
        return $response;

    }
}

The code may look messy somehow, because i kinda tried to make anything to make this works. (already tried to switch \Api\CreateAccountController to \CreateAccountController

My project path

enter image description here

Upvotes: 1

Views: 91

Answers (1)

odan
odan

Reputation: 4960

Change composer.json

"autoload-dev": {
    "psr-4": {
        "App\\Test\\": "tests"
    }
},
"autoload": {
    "psr-4": {
        "App\\": "src"
    }
},

Run composer update to ensure that all settings are applied.

Copy the file CreateAccountController.php to src/Api/

Change CreateAccountController.php

<?php

namespace App\Api;

Then fix the container entry like this:

$container[\App\Api\CreateAccountController::class] = function($c) {
// ...
}

Upvotes: 4

Related Questions