Phalconphp routing for multimodule application - php

I've created a multimodule application using phalconphp developer tool:
phalcon project <projectname> module
And I've added a backend module (the frontend is generated). Now I would like all backend routing do the following:
$route->add('/admin/:controller/:action/:param', array(
'module' => 'backend',
'controller' => 1,
'action' => 2,
'params' => 3,
));
But my routing also defines:
$router->setDefaultModule("frontend");
$router->setDefaultNamespace("Groendesign\Backend\Controllers");
And therefor when I browse to: http://myprojectname/admin it searches in my backend module for the frontend Namespaces, How should I proceed with this?
What I want to achieve is that every url that has the prefix /admin/ is send to the backend module. Using the url to define which controller, action and parameters.

I've fixed this by removing the setDefaultNamespace from my bootstrap and adding it to the Modules.php file in each Module. Thereby setting the DefaultNamespace only in the correct module.

Related

ZF2 'unable to render template' when shifting module names in the array in app config

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',
),
),

CakePHP Plugin - how to setup Apache Rewrite for vanity URL

Been trying to figure this out for a few days now with no joy. Here's my setup: I have a CakePHP install in
/home/user/tools/cakephp
and a plugin at
/home/user/tools/cakephp/app/Plugin/MyPlugin
The Apache server setup is such that I've set the DocRoot to /home/user/tools, so browsing to
http://myserver.com/cakephp/my_plugin
works fine, but now my client wants to set it up so that
http://myserver.com/product-name
serves up the CakePHP plugin, and all subsequent routes are honoured. Has anyone had any experience setting something like this up? Has to be Apache, unfortunately, and can be done with a mixture of config/.htaccess (clients constraints).
Thanks
Stephen
You are saying you have to do this via the Apache config or a .htaccess file, why? A plugin can have either his own Config/routes.php or you could configure the routing in your app wide app/Config/routes.php file. We are doing the same for a plugin we are using in our application.
What we did:
In the app wide routes file (app/Config/routes.php) we set a variable we use as our base url for accessing the plugin. We set it in a variable so we can easily switch when we get collisions with other controllers or plugins, so we want to keep that flexibility by doing the following:
# set the webroot for the plugin, for ajax calls and the sake of usability
$MyPluginBase = '/product-name';
# And we have this set just in case we need it somewhere in our application
Configure::write('MyPlugin.base', $MyPluginBase);
Then we configure our custom routes:
Router::connect($MyPluginBase . '/:name', array(
'plugin' => 'my_plugin',
'controller' => 'my_plugin_products',
'action' => 'products'
));
Router::connect($MyPluginBase . '/some/other/url/*', array(
'plugin' => 'my_plugin',
'controller' => 'my_plugin_some_controller',
'action' => 'whatever'
));
Now we can access the pugin via the 'product-name' url.
But, when you simply need a controller/action wide solution for this, you could accomplish this by using the following two routes:
Router::connect($MyPluginBase . '/:controller/:action/*', array(
'plugin' => 'my_plugin'
));
Router::connect($MyPluginBase . '/*', array(
'plugin' => 'my_plugin',
'controller' => 'my_plugin_main_controller'
));
Please note that the order of Router::connect methods matters!
ps. After reading the question again, I saw you have set your DocumentRoot wrong for production. Consult the following page in the cookbook for clarification: http://book.cakephp.org/2.0/en/installation.html#production
Adding the following line to the htaccess file should work
RedirectMatch 301 cake/my_plugin(.*) /product-name$1
The above would resolve:
http://myserver.com/cakephp/my_plugin to http://myserver.com/product-name
http://myserver.com/cakephp/my_plugin/somelink to http://myserver.com/product-name/somelink
When you say "all subsequent routes are honoured", I assume you mean that http://myserver.com/product-name/foo/bar will work the same as http://myserver.com/cakephp/my_plugin/foo/bar.
If that is the case, and you have mod_aliasinstalled, all you need to do is provide an Alias directive in httpd.conf:
Alias /product-name /cakephp/my_plugin
This should be completely transparent to CakePHP; it will be unaware that this mapping is happening.
If you want to prevent direct requests to http://myserver.com/cakephp/..., you could also add an external redirect:
Redirect 301 /cakephp/my_plugin /product-name
(from http://httpd.apache.org/docs/current/mod/mod_alias.html#alias)

Can't manage to create a static page with Zend Framework 2

I'm trying to make a website using the Zend Framework 2, but I have a simple problem driving me crazy.
I'd like to make a simple about-us.html page with static content because there is no need to do anything else than display html.
There is no need to create a Controller / model etc...
Maybe you have to question why you're using ZF in the first place. If you just want to create a static internet page, do that without a PHP framework!
If you didn't ask your question well and you're actually just adding a static page to an existing ZF application, why not just using the existing IndexController, add an empty action and add your static content to the corresponding .phtml?
Alternatively you can look at PhlySimplePage, a ZF2 module for the purpose of adding simple static pages to a ZF app. It's written by Matthew Weier O'Phinney, the lead dev of ZF.
As the readme says, this module eliminates the need to create a controller and an action. So all you need to do is create a route and a view script.
I know this question might be old, but I was in trouble with that as well.
Here is what I did to create a generic router for my application, so I could just add as many actions as I need and have then created as phtml files in my view
on module.config.php
'application' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:action][/]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
So basically what it does is gets any action you might have in your application and use as a page... like any other controller
Instead of http://yoursite.com/application/test
you can do now http://yoursite.com/test
if you have your testAction set in your indexController file.
Hope I had helped future people looking for this information.
You have to create controller and view file.
suppose there is a about us menu with link
<a href="<?php $this->url('aboutus'array('action'=>'aboutus'));?>">
About Us
</a>
Just go to aboutus view file and write static html code.
Thanks
Alok

Zend router, omit controller in URL for single module

I have a module based Zend application.
One of my modules, called portfolio has only one controller, called index. For this single module, I'd like my route to look like this:
$route = new Zend_Controller_Router_Route('portfolio/:action',
array(
'module' => 'portfolio',
'controller' => 'index',
'action' => 'index'
)
);
This works but messes up all links generated through Zend_Navigation.
Can this routing behavior be achieved, without messing up Zend_Navigation? (i.e. only inbound links are routed through this route. Outbound links are generated with the default route)
I can't use mod_rewrite.

Zend Regex > Route module problem

iam using zend framework to build a REST web service and i am using modules to separate my api versions, as i have mentioned here
Ex: "applications/modules/v1/controllers", "applications/modules/v2/controllers" have different set of actions and functionality. I have mentioned my default module as "v1" in my application.ini
I am using context switching along with Regex Routing as i have mentioned here in my accepted solution:
$router->addRoute(
'route1',
new Zend_Controller_Router_Route_Regex(
'api/([^-]*)/([^-]*)\.([^-]*)',
array(
'controller' => 'index',
'action' => 'index'),
array(
1 => 'module',
2 => 'controller',
3 => 'format'
)
));
This is my url: http://localhost/api/v1/tags.xml
"v1" indicates module. Now, coming to context switching, if the url has v1, it is going to v1 module's TagsController. But if the module in url is v2, i am getting an error such as:
The requested URL
/pt/public/index.php/api/v2/tags.xml
was not found on this server.
I could not understand why its blowing up. Is it because i have put the default module as v1? I am not able to change the module based on the url.
And this is my directory tree:
application
modules
v1
controllers
TagsController.php
v2
controllers
TagsController.php
library
My goodness... i have figured out the solution... the controller class name in the v2 module must be "V2_TagsController", not just "TagsController". Thank God, it is working now :)
See the class names for controllers below:
- application
- modules
- v1
- controllers
- TagsController.php (class TagsController)
- v2
- controllers
- TagsController.php (class V2_TagsController)
- library

Categories