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

**URL:** <https://discourse.slimframework.com/t/new-to-slim-is-model-and-controller-possible-in-slim/2080>\
**Category:** Questions\
**Created:** [December 6, 2017, 2:33pm UTC](https://discourse.slimframework.com/t/new-to-slim-is-model-and-controller-possible-in-slim/2080 "2017-12-06T14:33:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahesh](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@mahesh](https://discourse.slimframework.com/u/mahesh)\
**Post date:** [December 6, 2017, 2:33pm UTC](https://discourse.slimframework.com/t/new-to-slim-is-model-and-controller-possible-in-slim/2080/1 "2017-12-06T14:33:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [December 6, 2017, 2:42pm UTC](https://discourse.slimframework.com/t/new-to-slim-is-model-and-controller-possible-in-slim/2080/2 "2017-12-06T14:42:37Z")

</div>

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](https://github.com/akrabat/slim-bookshelf) example app as well as the [First Application Walkthrough](https://www.slimframework.com/docs/tutorial/first-app.html) which provide more complete examples.

---

<div class="post-metadata">

**Author:** ![mahesh](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@mahesh](https://discourse.slimframework.com/u/mahesh)\
**Post date:** [December 6, 2017, 4:54pm UTC](https://discourse.slimframework.com/t/new-to-slim-is-model-and-controller-possible-in-slim/2080/3 "2017-12-06T16:54:48Z")

</div>

Sir, Is there any ebook to start with slim?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [December 6, 2017, 6:58pm UTC](https://discourse.slimframework.com/t/new-to-slim-is-model-and-controller-possible-in-slim/2080/4 "2017-12-06T18:58:46Z")

</div>

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

Awesome Slim  
[https://github.com/xssc/awesome-slim](https://github.com/xssc/awesome-slim)

Tutorials  
[Getting Started with SLIM 3](https://scotch.io/tutorials/getting-started-with-slim-3-a-php-microframework)

[Creating your First Slim Framework Application](https://gist.github.com/odan/d2b889c350aa2ea0ff8e5ca93ce588a2)

---

<div class="post-metadata">

**Author:** ![mahesh](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@mahesh](https://discourse.slimframework.com/u/mahesh)\
**Post date:** [December 13, 2017, 8:49am UTC](https://discourse.slimframework.com/t/new-to-slim-is-model-and-controller-possible-in-slim/2080/5 "2017-12-13T08:49:49Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [December 13, 2017, 10:38am UTC](https://discourse.slimframework.com/t/new-to-slim-is-model-and-controller-possible-in-slim/2080/6 "2017-12-13T10:38:03Z")

</div>

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:

```php
$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:

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

```

And make sure the Action class has a \_\_invoke() method. e.g.

```php

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. 🙂 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.

```auto
$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.
