We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Better Phalcon Naming ?

Why all the complexity added ? Why not naming everything simpler in Phalcon ? Can't we have a choice to do this simpler ?

Original:

<?php

use Phalcon\Mvc\Controller;

class PostsController extends Controller

Request:

<?php

use Phalcon\Mvc\Controller;

class Posts extends Controller

You can simply lookup "Controller" after extends instead of having to append "Controller". Maybe even remove the need to use "use Phalcon\Mvc\Controller;" if nothing else used.

Same goes can be used for functions;

Original:

public function saveAction() {}

Request:

public function save() {}

You can simply lookup "function" instead of "Action" internnally.

Also, why are we forced to use controllers like "NameController" why not simply "Name.php" in /controllers/ folder.

Or even better if used with namespaces, folder stracture like; /App/Controllers/Name.php same goes for models like /App/Models/Name.php etc...

You can remove those suffix in the dispatcher:

see setActionSuffix



7.7k

@ Andres, could you give an example ? Thank you.

When registering the dispatcher service set the suffix to "":

<?php

use Phalcon\Mvc\Dispatcher as MvcDispatcher;

//...

$di->set(
    'dispatcher',
    function () {
        $dispatcher = new MvcDispatcher();
        $dispatcher->setActionSuffix("");
        $dispatcher->setDefaultNamespace('Phosphorum\Controllers');
        return $dispatcher;
    },
    true
);


7.7k
edited Apr '15

This will work for "NameController" and "nameAction" or just "Actions" on functions ? I think this should be a default, and if people need suffix they can append it. Right now its the other way around. Because having anything empty is just makes another useless line of code.



34.6k
Accepted
answer

Both of them, this way:

<?php

use Phalcon\Mvc\Dispatcher as MvcDispatcher;

//...

$di->set(
    'dispatcher',
    function () {
        $dispatcher = new MvcDispatcher();
        $dispatcher->setControllerSuffix("");       
        $dispatcher->setActionSuffix("");
        $dispatcher->setDefaultNamespace('Phosphorum\Controllers');
        return $dispatcher;
    },
    true
);


7.7k
edited Apr '15

Thank you for the example this is great work-around.

EDIT: I've also created a github issue for this @ https://github.com/phalcon/cphalcon/issues/10121

Thank you for the workarounds "Andres Gutierrez" this is great find :-)

edited Apr '15

in my opinion I don't think is a good idea to remove the suffix. This way you can see right away what is an action, what is a normal function and what is a simple class or a controller. Also there might be problems with class collisions

he pretty much wrote the whole thing :P

Thank you for the example this is great work-around.

EDIT: I've also created a github issue for this @ https://github.com/phalcon/cphalcon/issues/10121

Thank you for the workarounds "Andres Gutierrez" this is great find :-)