1

I'd like to make my first large project in php. I use Phalcon PHP and I created project structure using Phalcon Developer Tools. It something like: .

├── app
│   ├── cache
│   ├── config
│   │   ├── config.php
│   │   ├── loader.php
│   │   └── services.php
│   ├── controllers
│   ├── migrations
│   ├── models
│   └── views
├── index.html
└── public
    ├── css
    ├── files
    ├── img
    ├── index.php
    ├── js
    └── temp

I think i'll need some global function and classes. I'd like to implement for example Laravel's dd function to dumb variables and using this function like

dd($value); 

wherever i want. I also want to create some global classes to use theirs static functions. For example:

User::isLogged()

How to implement this in my project? Create directory functions or lib or indcude in app/? Is it a convention? Place global classes in individual folders? How to separate global functions and classes and register those in standard Phalcon loader and do it once for whole project?

Robin71
  • 383
  • 5
  • 26
  • Phalcon uses the MVC design pattern - your *"global classes"* will be models in /app/models (e.g. `User.php`); the controllers will be in /app/controller and be named for what they're for (e.g. `SignupController.php`) and views will be in subdirectories within /app/view, named for their paths (e.g. /app/view/signup/index.phtml for www.example.com/signup/). It's probably worth running through the tutorial : https://docs.phalconphp.com/en/latest/reference/tutorial.html – CD001 Jul 05 '16 at 09:30
  • @CD001 I have used Phalcon with small project. In app/models i had classes which represent tables in database. Now I'm talking about something different. Maybe User was a wrong example. I think about classes and functions which help me with develop e.g. Multimedia::uploadImage() or Captcha::checkCaptcha. It's classes not related with db. And what about dd function from example? – Robin71 Jul 05 '16 at 13:03
  • 1
    A model doesn't necessarily have to be related to a database table (outside of ORM); Multimedia would still be a model. Think of a model as modeling a "thing" or a business process - I found this to be a helpful read: http://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc – CD001 Jul 05 '16 at 13:42

1 Answers1

3

Good thing about Phalcon is that you have the freedom to organize your project in the way it best fits your current situation.

A more general approach I'm using in most of my projects is to register the most used namespaces in the autoloader. In my case I'm using multi module structure and this is done in the Module.php file for the given module.

Module class:

class Module
{
    public function registerAutoloaders($di)
    {
        $config = $di->getConfig();
        $loader = new \Phalcon\Loader();
        $namespaces = [
            'Frontend\Controllers' => __DIR__ . '/controllers/',
            'Frontend\Forms' => __DIR__ . '/forms/',
            'Models' => $config->site->path->common . 'models/',
            'Helpers' => $config->site->path->common . 'helpers/',
        ];
        $loader->registerNamespaces($namespaces);
        $loader->register();
    }
}

Helpers in my case are files which are not models and serve for something specific. For example I have a Files helper which holds functions for manipulating the file system. I've got a Helper for handling string operations like slugalization, latinization and so on...

I've also have a Lib folder in which I put my public libraries like PHPMailer, BrowserDetect, ImageProcessing libraries and so on.

And now about the global functions like Laravel's dd(). I have a small file which I include in the bootstrap file or your index.php. It contains 1-2 global functions like:

function d($what) 
{
    echo '<pre>';
    print_r($what);
    die('</pre>');
}

In my case there are not that many global functions which I want to use everywhere easily, like above one for debugging. Rest of the stuff I've put in the Helper files mentioned above.

Hope I helped and would be glad to hear someone else's opinion on this.

Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32