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

How to make a controller in a subfolder work ?

I am new to PHALCON

I create a project, and this is the folder struture:

wsph
+--app
¦  +--config
¦  +--controllers
¦  ¦  +--wsget
¦  ¦     +-- WmsController.php
¦  ¦  ¦
¦  +--library
¦  +--migrations
¦  +--models
¦  +--views
¦     +--layouts
+--cache
+--public

My Router file is:

<?php
$router = $di->getRouter();

// Define your routes here
$router->add(
    "/wsget/:controller/:action",
    [
        "controller" => 1,
        "action"     => 2,
        "namespace" => "App\Controllers\Wsget"
    ]
);
$router->handle();

My Loader file is:

<?php
$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerDirs(
    [
        $config->application->controllersDir,
        $config->application->modelsDir,
    ]
);

$loader->registerNamespaces(
    [
       'App\Controllers\Wsget' => 'app/controllers/wsget',
    ]
);

$loader->register();

My controller is :

namespace App\Controllers\Wsget;

use Phalcon\Mvc\Controller;

class WmsController extends Controller
{
    public function SalesAction() { echo json_encode( [ 'erro' => 8500, 'msg' => 'OK!!!!'] ); }
}

The Url i need to access for this controller is: localhost:8083/wsget/wms/sales

How to make this URL work?

Thanks in advance



85.5k
edited Aug '18

that breaks the concept i think. Perhaps what are you trying to accomplish is just unnessery ?

I try to migrate a system, make with other framework. Becouse a i need support higth work load.

You can create controllers wherever you want, in whatever folders you want, not sure what's your problem?

edited Aug '18

Access this url : localhost:8083/wsget/wms/sales
returns :

Not Found

The requested URL /wsget/wms/sales was not found on this server. I access localhost:8083 is OK

the problem setup the routes to accesslocalhost:8083/wsget/wms/sales

edited Aug '18

No, it's problem with your apache/nginx configuration. If you added this route it should work totally fine, anything not working is about your server configuration.

one question

webroot   -> web server root 
         wsph   -- project root
                 public    public folder

the public folder is the root in my Virtual Host, is this necesary ?

<VirtualHost *:8083>
    ServerAdmin [email protected]
    DocumentRoot "F:/webroot/wsph/public"
    ServerName localhost:8083
    ErrorLog "logs/error_wsph.log"
    CustomLog "logs/access_wsph.log" common
</VirtualHost>

Is same wrong with this?

I would suggest using nginx. Just check docs of phalcon how to configure it for apache. Right now your code is fine, problem is with apache configuration of vhost, like your urls aren't rewritten to index.php

OK, i make some change and now :

App\Controllers\Wsget\WmsController handler class cannot be loaded

0 [internal function]: Phalcon\Mvc\Dispatcher->_throwDispatchException('App\Controllers...', 2)

1 [internal function]: Phalcon\Dispatcher->dispatch()

2 F:\webroot\wsph\public\index.php(44): Phalcon\Mvc\Application->handle()

3 {main}

Your controller is not registered properly, you have issue with paths or something else in loader or you load it in wrong place.

Index has:

$di = new FactoryDefault();

include APP_PATH . '/config/router.php';

include APP_PATH . '/config/services.php';

$config = $di->getConfig();

include APP_PATH . '/config/loader.php'; This is the order.

$loader = new \Phalcon\Loader();

$loader->registerDirs(
    [
        $config->application->controllersDir,
        $config->application->modelsDir,
    ]
);

$loader->registerNamespaces(
    [
       'App\Controllers\Wsget' => 'app/controllers/wsget',
    ]
);

$loader->register();

Controller is:

namespace App\Controllers\Wsget;

use Phalcon\Mvc\Controller;

class WmsController extends Controller
{
    public function SalesAction() { echo json_encode( [ 'erro' => 8500, 'msg' => 'Cheguei!!!!'] ); }
}

the folder estructure is in the first posr

SOLVED

Loader file Change from this

$loader->registerNamespaces(
    [
       'App\Controllers\Wsget' => 'app/controllers/wsget',
    ]
);

To this :

$loader->registerNamespaces(
    [
       'App\Controllers\Wsget' => __DIR__ . '/../controllers/wsget',
    ]
);

Also you should avoid both registersDirs and registerNamespaces, you should stick to registerNamespaces pretty much.

registerDirs was create with the project.



145.0k
Accepted
answer

You can remove it, it's not necessary.