Search code examples
phprestfat-free-framework

Fat-Free with Cortex Models and REST


I'm trying to use FatFree and Cortex plugin within my REST API. I seem to have some challenges, at first I was challenged with the namespaces, which I was trying to find how to solve that. And then once I set that up, it was giving issues with method get() already existed. I later tried to not use namespaces to see if that would help, but continued with errors.

I am hoping to get some clarification on how to setup Cortex + F3 + REST API and having auto mapping routes. What am I doing wrong on setting up the model for Cortex? Namespaces doesn't really matter, if I use them or not (personally). Just would like it to work.

My Autoloader is

$f3->set('AUTOLOAD','app/controllers/;app/models/');

Here is my file setup enter image description here


Solution

  • Multiple issues here.

    • don't mix different upper-case lower-case namings for files and folder or the autoload will not find it.. use file path App/Controllers/Contacts.php when your namespace is \App\Controllers\Contacts or name all files and folder in lower-case.. but don't mix them
    • When you set your namespace at \App\Controllers, \App\Models, etc. your AUTOLOAD setting should only be '' to use the project root. If you set AUTOLOAD to app/, then your namespace begins at this folder, so it's just namespace Controllers. Same for Models
    • When you are within a namespace, you have to add a leading \ to your namespace of other files, so within namespace \App\Controllers in you must use new \App\Models\ContactsModel .. if you only write new App\Models\ContactsModel, it'll look for a file at app/controllers/app/models/contactsmodel.php

    I would suggest:

    • change folder names to: app/Models and app/Controller, etc
    • leave file names for classes the same as the class name so class ContactsModel -> ContactsModel.php
    • set AUTOLOAD to app/ only
    • use namespaces like folders within app/, so ContactsModel -> namespace Model; and Contacts -> namespace Controllers