I am using routing component to load controllers, and if i use just
$routes->add(
'index',
new Route('/', array('_controller' => 'indexAction'))
);
my "project" perfectly loads indexAction function, but if i try something like this
$routes->add(
'index',
new Route('/', array('_controller' => 'Test::indexAction'))
);
it says
Uncaught exception 'InvalidArgumentException' with message 'Class "Test" does not exist.'
But i cant find where must be my controllers, or how they must be included to be loaded successfully.
If this helps, at this moment i am using composer autoload with PSR-0 standart.
Like its said on the Symfony doc about routing, you have to
name your controller with this pattern :
bundle:controller:action
"Full" path solved the problem
new Route('/', array('_controller' => 'Levelup\\Controller\\Test::indexAction'))
Test::indexAction changed to Levelup\Controller\Test::indexAction
Related
Good day.
I've just started learning ZF2, replicated the Album example step-by-step, and then decided to make it a bit more interesting by adding user registration, and then make the two work together in some way, so i added a new 'Auth' module.
So, when i only had one module in the module list (aside from the Application module) in application.config.php, it was all fine, but when i added the Auth module to the list like so:
'modules' => array('Application', 'Album','Auth',)
i got the following error when trying to access any views from the album module which was working absolutely fine prior to this change:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "album/album/index"; resolver could not resolve to a file
But when i changed the order in which the modules are presented in this array like so:
'modules' => array('Application', 'Auth','Album',)
not a single view (and it has only one) from the Auth module could be rendered, while the Album module was fine.
Zend\View\Renderer\PhpRenderer::render: Unable to render template "auth/user/index"; resolver could not resolve to a file
Both of these views exist at these locations, but the renderer doesn't see them for some reason.
You can see the project's contents here.
Thank you for any tips in advance.
Looks like you copy pasted the the view manager config for Auth module.config.php.
This should be auth rather than album for your templates to work correctly.
'view_manager' => array(
'template_path_stack' => array(
'auth' => __DIR__ . '/../view',
),
),
I'm trying to define RESTful routes for sub directory controllers. I want to be able to create routes for the url at admin/questions/*. My controller is Admin_QuestionsController:
- application
- controllers
-Admin
QuestionsController.php (class Admin_QuestionsController)
Below is how I'm declaring my RESTful route for this controller:
$restRoute = new Zend_Rest_Route($front, array(), array(
'admin' => array('questions')
));
$router->addRoute('rest', $restRoute);
..from the documentation I can't see what I'm doing wrong - http://framework.zend.com/manual/1.12/en/zend.controller.router.html#zend.controller.router.routes.rest. However, I get the following error:
Invalid controller specified (admin)
I can get the routes to work when I declare then not as Rest routes:
$router->addRoute('admin_questions',
new Zend_Controller_Router_Route( '/admin/questions', array(
'controller' => 'Admin_Questions',
'action' => 'index')
)
);
..so I don't think I've have the folder structure wrong, or the class name. But, I need RESTful routes which I'm unable to achieve.
The Zend_Rest_Route route like you have defined, works if you have Zend modules enabled. The documentation mentions "translating the HTTP method and the URI to a module, controller and action". To enable modules, add the following two lines in your application.ini:
resources.modules[] =
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
Then create a directory in application/modules named admin/controllers, and create your QuestionsController in application/modules/admin/controllers/QuestionsController.php.
The rest of your application should (hopefully) still work as the default module.
I try to use a router with phalcon. This is how it is included in index.php right after registering the 'events manager':
$di->set('router', function(){
require __DIR__.'/../app/config/routes.php';
return $router;
});
and this is how the routes.php looks like:
<?php
$router = new Phalcon\Mvc\Router(false);
$router->add("/", array(
'controller' => 'index',
'action' => 'index'
));
$router->add("/topics", array(
'controller' => 'wurst',
'action' => 'index'
));
$router->handle();
return $router;
The website reacts as if the router was not existent. /topics and topics say this:
TopicsController handler class cannot be loaded
and I also cannot use a die("test"); function inside routes.php . nothing happens.
I also tried to activate it without a separate file, but the result was the same :(
(The sample web-application INVO was used as starting point for my site )
$router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI); will use default $_SERVER['REQUEST_URI']
If your index/index action is working when you access domain.com/index.php, check that you are using proper uri source, if using nginx or php built-in server you might have some problems with routing and $_GET['_uri'] which phalcon use for handling uris.
can find more about it on phalcon router documentation about uri sources -> http://docs.phalconphp.com/en/latest/reference/routing.html#uri-sources
now it seems to work:
Action 'route404' was not found on handler 'index'
the problem was , that I put the function to set the router in index.php within "set dispatcher function". ..did not see the closing braces.
So I've been reading a ton of stackoverflow and phalcon forum threads.. (I'm starting to hate this framework), but nothing seem to work and it doesn't explain why like Laravel does, for example.
I'm just trying to be able to operate with this application structure:
As you can see, all I want is to use namespaced controllers in subfolders to make more order for my code.
According to all explanations, here's my loader.php:
<?php
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir,
$config->application->pluginsDir
)
)->register();
AFAIK, Phalcon should traverse all subfolders for not found classes when used via registerDirs.
Then I define my routes to specific controller after the main route to index controllers in base directory:
<?php
$router = new Phalcon\Mvc\Router(false);
$router->add('/:controller/:action/:params', array(
'namespace' => 'App\Controllers',
'controller' => 1,
'action' => 2,
'params' => 3,
));
$router->add('/:controller', array(
'namespace' => 'App\Controllers',
'controller' => 1
));
$router->add('/soccer/soccer/:controller', array(
'namespace' => 'App\Controllers\Soccer',
'controller' => 1
));
$router->add('/soccer/:controller/:action/:params', array(
'namespace' => 'App\Controllers\Soccer',
'controller' => 1,
'action' => 2,
'params' => 3
));
return $router;
And one of my controllers look like:
<?php namespace App\Controllers\Soccer;
use App\Controllers\ControllerBase as ControllerBase;
class IndexController extends ControllerBase
{
public function indexAction()
{
}
}
What's wrong here? Default top namespace is not registered? Am I missing something?
This just doesn't work. When I try to open myserver.com/soccer which I expect to go to app/controllers/soccer/IndexController.php, but instead it tells me:
SoccerController handler class cannot be loaded
Which basically means it's looking for SoccerController.php in /controllers directory and totally ignores my subfolder definition and routes.
Phalcon 1.3.0
Stuck on this for a week. Any help - Much appreciated.
I was having a problem with loading the ControllerBase and the rest of the controllers in the controllers folder using namespaces. I was having a hard time since other example projects worked fine and I realized that I was missing a small detail in the despatcher declaration where I was supposed to setDefaultNamespace
(ref: https://github.com/phalcon/vokuro/blob/master/app/config/services.php)
$di->set('dispatcher', function () {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace('App\Controllers');
return $dispatcher;
});
or you can specify it directly on the routing declaration file like this
$router->add("/some-controler", array(
'namespace' => 'App\Controllers'
'controller' => 'some',
'action' => 'index'
));
that should work as well, it might be a bit confusing at first with namespaces but once you get a hang of it you will love this extremely fast framework
It looks like your namespaces have capitals
App\Controllers\Soccer
and your folder structure does not
app\controllers\soccer
In my app I have controllers with a namespace and I have just tried changing the case of the folders so they don't match and I get the missing class error.
Of course it does raise a question, how many controllers are you planning on having that namespacing them is worthwhile? Are you grouping controllers and action by function or content? I used to have loads of controllers in Zend, but now I have grouped them by function I only have 16 and I am much happier. e.g. I suspect soccer, rugby, hockey etc will probably be able to share a sport controller, articles, league positions etc will have a lot of data in common.
ps Don't give up on Phalcon! In my experience it is sooo much faster than any other PHP framework I have used it is worth putting up with a lot :)
Are you sure Phalcon traverses subfolders when registering directories to the autoloader? Have you tried adding a line to your autoloader which explicitly loads the controllers\soccer directory?
Alternatively, if your soccer controller is namespaced, you can also register the namespace: "App\Controllers\Soccer" => "controllers/soccer/" with the autoloader.
I really can't figure this out:
I've created a file called User.php in application/models. The classname in it is Model_User.
When I try to create an object in my Controller, I get this error:
Fatal error: Class 'Model_User' not found in C:\xampplite\htdocs\code\application\controllers\IndexController.php on line 14
I googled around, and found this code, which is supposed to autoload controllers for me, it is located in bootstrap.php The code isn't working though. The example that used this code was working with ZF 1.8 so that might be the reason but I can't figure it out.
How should I autoload my models?!
private function _initAutoload(){
$modelLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH
));
return $modelLoader;
}
Any ideas?
The important bit in the answer to the question I linked above is the namespace:
$resourceLoader->addResourceTypes(array(
'model' => array(
'namespace' => 'Model',
'path' => 'models'
)
));
The namespace parameter tells the Autoloader to look in the defined path (relative to basePath) when encountering a class that starts with Model_. You have the first part right, but you're missing the call to addResourceTypes.