I wanna match an url like this http://localhost/sunnetwap/1-hot and rewrite into this
http://localhost/sunnetwap/?c=1&s=hot.
In my Bootstrap and and _initRoutes function i wrote:
$frontController = Zend_Controller_Front::getInstance();
$route = new Zend_Controller_Router_Route_Regex(
"([0-9]*)-([a-z]*)",
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
),
array(
1 => 'c',
2 => 's'
));
$frontController->getRouter()->addRoute('home',$route);
but it doesn't work. Can you help me?
The issue is probably related to the base url of your app.
It is possible that you have deployed your app at the base url http://localhost/. In this case, your routes would need to include the sunnetwap portion.
But I'm guessing that your app is deployed to http://localhost/sunnetwap/. In that case, your route is fine, but it's likely that you either have a sunnetwap alias on localhost or that the whole app is dropped into into your <docroot>/sunnetapp and that you have moved contents of the public folder up a level with corresponding changes to .htaccess. Both of these will potentially have impact on the routing.
See this answer for some ideas on how to deploy to a sub-directory when you can't map a virtual host to your app.
Related
I am using INVO example app from here:
https://github.com/phalcon/invo
I have copied all the files and set the db and base url.
It works, I can login etc.
however, I wanted to learn how to use redirects e.g.
I would like to use contact-us instead of contact without changing the name of the controller.
So, I created a file routes.php inside of the app/config folder and put this inside:
<?php
$router = new Phalcon\Mvc\Router(false);
$router->add('/contact-us', array(
'controller' => "contact",
'action' => "index"
))->setName('contact');
$router->handle();
return $router;
and I have created this in my bootstrap file index.php in the public root directory
$di = new \Phalcon\DI\FactoryDefault();
$di->set('router', function(){
require __DIR__.'/../app/config/routes.php';
return $router;
});
However, it's not working and when I try to access http://localhost/test/contact-us it works, but http://localhost/test/contact-us stopped working and I am redirected to the homepage.
If I (comment the route) do this:
/*$router->add('/contact-us', array(
'controller' => "contact",
'action' => "index"
))->setName('contact');*/
$router->handle();
return $router;
Neither http://localhost/test/contact-us nor http://localhost/test/contact-us works ;(.
If I uncomment it back. contact-us works but contact don't.
I guess it's b/c of ->setName('contact') and it's stored in the memory or in some file.
how to get it back to the original state and "unset" that?
You can create two routes and each will work:
$router->add('/contact', array(
'controller' => "contact",
'action' => "index"
));
$router->add('/contact-us', array(
'controller' => "contact",
'action' => "index"
));
Or use groups of routes.
So, if you have ContactController and don't want to access it using /contact url you should add router to the DI container. After that the pages will be available only via router and default /controller/action request format will not work.
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)
It's probably late and I have missed this off by a long shot.
I am trying to create a cleaner url structure; so rather than having
/index/about
/index/news
I have
/about
/news
I came across a post on this site which used the following:
public function _initCustomRoute()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route(':action', array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('default', $route);
}
It rewrites the url within my navigation. So I have created the relevant action and view (tested without the custom route) but I am getting:
Not Found
The requested URL /path/to/public/index.php was not found on this server.
I assume this is a thing that apache does on a windows file system by not adding the drive letter.
I've not touched the .htaccess file.
Any ideas?
Found out why! You must use virtual hosts rather than an alias as I was using.
Full guide here: http://blog.ryantan.net/2010/03/setting-up-wamp-for-zend-framework-projects/
Very likely I'm going about this in the wrong way entirely. I'm completely new to the framework..
The site I am developing has two "parts" that are mainly separate. An informational/community half, and a commerce half. I'm using the following directory structure:
--application
----default
------controllers
------layouts
------models
------views
----store
------controllers
------layouts
------models
------views
--config
--library
--public
I would like to have a URL structure when browsing for products as follows:
/view/category/model/revision
This would pull up a specific product/revision - but I would like to back-track as well (browsing all revisions, all models, etc). I can't figure out how to achieve this.. My route is setup like this:
Bootstrap.php
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$route = new Zend_Controller_Router_Route(
'view/:cid/:sku/:rev',
array('module' => 'store', 'controller' => 'index', 'action' => 'index')
);
$router->addRoute('view', $route);
This works fine for pulling up a specific product, but throws an exception (it reverts to the default module and complains that the controller 'view' does not exist) when leaving out any of the 3 labeled parameters. Is it possible to put in optional labels, where it would continue to use the view controller under the store module for 1-3 parameters? Am I missing the point?
I found nothing in the framework docs, but I wouldn't be surprised if I just couldn't find the page.. There's something about the Zend Framework documentation that drives me crazy.
Thank You
I'm not really a ZendFramework guy, but it's obvious the missing parameters are causing the issue. Routes are matched in reverse order. Could it be passing a NULL value to the view when 3 parameters are passed and it is expecting 4?
What if you tried something like:
$route = new Zend_Controller_Router_Route(
'view/:cid/:sku/:rev',
array('module' => 'store', 'controller' => 'index', 'action' => 'index', 'cid' => 0, 'sku' => 0, 'rev' => 0)
);
It should pass default values if they are not provided.
When I access my site on MAMP like so, it works great
localhost/site/about-us/
When I upload it to my remote server, and access it like this
http://www.server.com/site/about-us/
all requests go back to the 'default' set up in bootstrap.php.
Here is my route setting.
Route::set('default', '(<page>)')
->defaults(array(
'page' => 'home',
'controller' => 'page',
'action' => 'index',
));
The problem is, whenever it gets uploaded to my server, any request like /about-us/ is always defaulting to home as specified when setting the route. If I change that default to 'about-us', every page goes to 'about us'.
Does anyone know what may be causing this? Thanks
UPDATE
Here is a hack that works, but is sure ugly as hell. Still I'd prefer to know why it doesn't work as per expected.
// Hack because I can not get it to go to anything except 'default' below...
$uri = $_SERVER['REQUEST_URI'];
$uri = str_replace(url::base(), '', $uri);
$page = trim($uri, '/');
if ( ! $page) $page = 'home';
Route::set('default', '(<page>)')
->defaults(array(
'page' => $page,
'controller' => 'page',
'action' => 'index',
));
Your code is basically a catch all route (it's being matched for all requests). You should restrict it like so.
Route::set('static', '(<page>)', array('page' => 'about-us'))
->defaults(array(
'controller' => 'page',
'action' => 'index',
));
The 3rd parameter is a regular expression which defines what the route should match.
That route will route everything matched in the regular expression to the page controller and its index action.
You can then use $page = $this->request->param('page'); in your action.
Are you not mistaking $page for $action?
If I try this, it works just fine. Here's my controllers action method:
public function action_index($page = NULL)
{
var_dump($page);
}
If I browse to
localhost/site/blup
I see see a nice
string(4) "blup"
being echo'd. I have the default route setup identical to yours.
It sounds like Kohana's auto-detection of the URL isn't working for your server setup... What web server is it failing on?
You could alter the Request::instance()->execute()... line in the bootstrap to start with:
Request::instance($_SERVER['REQUEST_URI'])->execute()...
That will ensure it uses the correct URI..
That being said ... as The Pixel Developer says, your route looks.. odd .. to me ;)
But - since it works on MAMP - The route is likely not the issue.