New to slim - Is Model and Controller possible in slim?

I am using codeigniter for my projects.
I need to develop API.

I am using react js for view and decided to use Slim Framwork.
I am reading about Slim in various websites.
For example,

$app->post('/books', function($request){

 require_once('db.php');

 $query = "INSERT INTO library (book_name,book_isbn,book_category) VALUES (?,?,?)";

 $stmt = $connection->prepare($query);

 $stmt->bind_param("sss",$book_name,$book_isbn,$book_category);

 $book_name = $request->getParsedBody()['book_name'];

 $book_isbn = $request->getParsedBody()['book_isbn'];

 $book_category = $request->getParsedBody()['book_category'];

  $stmt->execute();

});

Everything is going in this code block.
Logic, inclusion of db, writing query

Is there a better way to write application using SLIM? Something like in Codeigniter?

Slim is … well … very slim. As such it is basically just a controller. You’re right, the db inclusion in that controller action isn’t a great place to put it.

But being slim, you can add your own Model/ORM, just pick the one you like to use. For examples of more full MVC style setups, have a look at the Slim-bookshelf example app as well as the First Application Walkthrough which provide more complete examples.

Sir, Is there any ebook to start with slim?

Here you can find a list of tutorials and other resources for the Slim micro framework:

Awesome Slim
https://github.com/xssc/awesome-slim

Tutorials
Getting Started with SLIM 3

Creating your First Slim Framework Application

1 Like

Slim Bookshelf is giving me good start. I have to go through other examples .

// @codingStandardsIgnoreStart
$container[Bookshelf\Action\ListAuthorsAction::class] = $authorActionFactory(Bookshelf\Action\ListAuthorsAction::class);
$container[Bookshelf\Action\GetAuthorAction::class] = $authorActionFactory(Bookshelf\Action\GetAuthorAction::class);
$container[Bookshelf\Action\CreateAuthorAction::class] = $authorActionFactory(Bookshelf\Action\CreateAuthorAction::class);
$container[Bookshelf\Action\EditAuthorAction::class] = $authorActionFactory(Bookshelf\Action\EditAuthorAction::class);
$container[Bookshelf\Action\DeleteAuthorAction::class] = $authorActionFactory(Bookshelf\Action\DeleteAuthorAction::class);
// @codingStandardsIgnoreEnd

I dont understand this. Do i need to include all my classes here?
Is it actually loading all the php files or just getting reference for them?

How does the performance change if i add 50 more classes here?

use Nocarrier\Hal;
What does Hal do here?

Thanks for helping me :slight_smile:

Well, your example does not scale very well because it slows down your application. All you need to do is create an instance on demand. Simply use the anonymous functions (closure) as “factory” to load only the objects you really need. Example:

$container[Bookshelf\Action\ListAuthorsAction::class] = function (Container $container) {
    // This here is the factory
    return new Bookshelf\Action\ListAuthorsAction($container->get('view'), $container->get('db'));
};

Edit:

I dont understand this. Do i need to include all my classes here?

Not really, if you make use auf the default behavior of slim. Just add a route in your routes.php like this:

$app->get('/', App\Action\HomeIndexAction::class);

And make sure the Action class has a __invoke() method. e.g.


namespace App\Action;

use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;

class HomeIndexAction
{
    public function __construct(Container $container)
    {
        // TODO: Implement __construct() method.
    }

    public function __invoke(Request $request, Response $response)
    {
        // TODO: Implement __invoke() method.
        $response->getBody()->write('Hello World');

        return $response;
    }
}

Then you don’t need a container entry for the routes. :slight_smile: However, if you do not want to pass the entire container into the action, you should use callbacks (first example) and define all dependencies explicitly in the constructor of the action class.

Is it actually loading all the php files or just getting reference for them?

This looks very suspicious to me.

$authorActionFactory(Bookshelf\Action\ListAuthorsAction::class);

How does the performance change if i add 50 more classes here?

I think this will not perform very good if you have 50 or more routes. Please read my comment from above.

1 Like