# Newbie code layout question

**URL:** <https://discourse.slimframework.com/t/newbie-code-layout-question/350>\
**Category:** Questions\
**Created:** [June 1, 2016, 1:24pm UTC](https://discourse.slimframework.com/t/newbie-code-layout-question/350 "2016-06-01T13:24:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeonardChallis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/leonardchallis/32/41_2.png) [@LeonardChallis](https://discourse.slimframework.com/u/LeonardChallis)\
**Post date:** [June 1, 2016, 1:24pm UTC](https://discourse.slimframework.com/t/newbie-code-layout-question/350/1 "2016-06-01T13:24:16Z")

</div>

Hi all,

I wondered if I could get some advice on the “best” way to lay out my code. My long-term goal is to rewrite much of our current out of date system in to an API. api.domain.tld will be the top level domain and different features will be specified with groups. The first project is the orders returns system and will be found at api.domain.tld/returns

I am using Propel ORM and practically all model code will be in the generated classes.

My own classes are in `src/`, propel classes are in `generated-classes/` and slim and other components are in `vendor/`, installed with composer. My `index.php` file lives in `public/`, where I setup `$app`. Inside there I will have several groups, for instance my returns group:

```
$app->group('/returns', function () use ($app) {
    $app->get('/list', function ($request, $response) {
        // ...
    });
    $app->post('/add', function ($request, $response) {
        // ...etc
    });
});

```

Now, requests will need just a little but of model code, for instance instantiating an `OrderReturn` object and either outputting it as JSON or updating some values and saving. Would it be best to put the model code directly in there, or would it be best to have an `invokable` class and add it as middleware to the group? For instance a `Model` class that performs actions depending on the `$request`. Or is there a better way here, bearing in mind that eventually there will be a lot of groups and routes. I actually don’t mind them all being in index.php as such, but that could get very big if the actual code is in the index.php file.

I hope that makes sense, please ask if I can give more information as I’m probably missing something 🙂

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [June 1, 2016, 7:49pm UTC](https://discourse.slimframework.com/t/newbie-code-layout-question/350/2 "2016-06-01T19:49:10Z")

</div>

I think your current approach is not very scaleable (from a code-perspective). I would suggestion you implement controller classes, whose methods you then put in your route definition. It’s definitely much more readable than putting code directly into the route definitions.

My routes.php looks like this (and I would say, is very readable):

```
$app->get ('/faq', 'App\Controller\UserController:faq')->setName ('faq');
$app->get ('/helpdesk', 'App\Controller\UserController:helpdesk')->setName ('helpdesk');
$app->get ('/helpdesk/{id}', 'App\Controller\UserController:helpdeskTopic')->setName ('helpdesk_topic');
$app->get ('/login', 'App\Controller\UserController:login')->setName ('login');

```

Hopefully this answers your question and helps.

---

<div class="post-metadata">

**Author:** ![LeonardChallis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/leonardchallis/32/41_2.png) [@LeonardChallis](https://discourse.slimframework.com/u/LeonardChallis)\
**Post date:** [June 2, 2016, 10:38am UTC](https://discourse.slimframework.com/t/newbie-code-layout-question/350/3 "2016-06-02T10:38:02Z")

</div>

Thanks, I just read some of your other posts with similar/related information and I agree, especially in my case as these “controller actions” can work directly with the Propel model classes - it all seems need and compartmentalised 🙂

Thanks again

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [June 2, 2016, 12:40pm UTC](https://discourse.slimframework.com/t/newbie-code-layout-question/350/4 "2016-06-02T12:40:03Z")

</div>

> these “controller actions” can work directly with the Propel model classes

While I don’t know Propel, I’d be careful about binding your routes directly to Model classes. The Job of a controller function is to acquire, validate, prepare data, etc. This of course all depends on your use case, but I’d say that there is a pretty good architectural case for not binding routes to your Models directly because even in the most basic case, your controller needs to acquire the data from somewhere and your Model has no business knowing about how to acquire this data.

---

<div class="post-metadata">

**Author:** ![LeonardChallis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/leonardchallis/32/41_2.png) [@LeonardChallis](https://discourse.slimframework.com/u/LeonardChallis)\
**Post date:** [June 2, 2016, 1:35pm UTC](https://discourse.slimframework.com/t/newbie-code-layout-question/350/5 "2016-06-02T13:35:07Z")

</div>

I get your point - thanks. I think I explained it poorly. I meant I can call call model functions from the controller actions, something like:

```php
public function view($request, $response, $args)
{
    $return = OrderReturnQuery::create()->findPK($args['id']);
    return $response->withJson($return);
}

```
