I am currently outputting the url of a particular module like so:
$this->view->url(array('controller'=>'index','action'=>'index','module'=>'somemodule'))
The problem is when I view:
/somemodule/action1/paramid/paramval
The url is showing as:
/somemodule/index/index/paramid/paramval
When all I really want is:
/somemodule/
Any ideas? I have used the above url array because I am trying to force it to display the correct url. sadly, simply array('module'=>'somemodule') doesn't work...
Weird, I would've thought it defaults to index/index if they aren't specified. You could try setting up something with Zend_Controller_Router_Route;
$router = $ctrl->getRouter(); // returns a rewrite router by default
$router->addRoute(
'user',
new Zend_Controller_Router_Route('somemodule',
array('module' => 'somemodule',
'controller' => 'index',
'action' => 'index'))
);
You will however need to set up routes to capture the :id/:val into variables. You can do this with another addRoute(), naming each variable prepending a colon.
$router->addRoute(
'user',
new Zend_Controller_Router_Route('somemodule/:id/:val',
array('module' => 'somemodule',
'controller' => 'index',
'action' => 'index'))
);
Then you should be able to view the module without the index/index in the URL which might fix the problem with the url helper.
More info here http://framework.zend.com/manual/en/zend.controller.router.html
Related
I would like to know how can i do this in phalcon. I have a web site build with phalcon. All is working great now i stumbled upon a problem, here is what i need.
When a user clicks on a post that was created by another user. It takes him to this post with pictures and all things he entered to DB. I would like that in browser the name of this view is not like www.website.com/posts/index but that it is like www.website.com/posts/Nameofthepost, and like that for each other postings on the website. So that all posts (really ads) show their name up in browser. I hope i wrote everything understandable.
Appreciate all suggestions
That has to do with routing doesn't it? I modified this from my own code, I used grouping, you don't have to. I didn't test this code.
// routes.php
$router = new \Phalcon\Mvc\Router();
$router->setDefaultModule("__YOUR_MODULE__");
$router->removeExtraSlashes(true);
... your other routes ...
// posts group
$posts = new \Phalcon\Mvc\Router\Group(array(
'module' => '__YOUR_MODULE__',
'controller' => 'posts',
'action' => 'index'
));
// All the routes start with /post
$posts->setPrefix('/post');
$posts->add('/{postName}/:params', array(
'action' => 'index',
'params' => 2
));
// Maybe this will be enough for your needs,
// the one above has a catch all params, which
// has to be manually parsed
$posts->add('/{postName}', array(
'action' => 'index',
));
$posts->add('[/]*', array(
'action' => 'index',
));
$router->mount($posts);
unset($posts);
... other routes ...
return $router;
On your controller, you can get the postName param this way:
$this->dispatcher->getParam('permaPath');
As shown in the phalcon routing documentation, you can use regex in your routing config, something like this?
$posts->add('/{postName:[-0-6_A-Za-z]+}/:params', array(
'action' => 'index',
'params' => 2
));
So, only -_, 0-9, A-Z, a-z allowed for postName. If the URL had a comma in there or something, then route doesn't match, 404 page not found.
I am new to Zend Framework. I have a page that is:
http://localhost/demo/public/index/index/catid/art
I want to change that into
http://localhost/demo/public/art
I have no idea how to do it.
Also, why does it put index twice? Even my pagination has it, like:
http://localhost/demo/public/index/index/page/2
In my opinion is a little bit annoying. I would like the pagination to be
http://localhost/demo/public/page/2
Is there a way to do that? Thanks!
The default route works by using:
/module/controller/action
So if you have a module called "default", and your controller called "index", and an action called "index", then the most verbose way to refer to that specific action would be:
/module/controller/action
In order to set up a route you would use:
$route = new Zend_Controller_Router_Route(
'page/:page',
array(
'module' => 'default'
'controller' => 'index',
'action' => 'index'
),
array(
'page' => '\d+'
)
);
You could then get the page param in your controller using
$this->getRequest->getParam("page");
I'am using CakePHP 2.0 for a small admin section of a website.
If the user is logged in correctly, I redirect them to the admins dashboard.
I do this as following:
$this->redirect(Router::url(array('controller' => 'admin', 'action' => 'dashboard')));
The redirect is done correct, but for some reason the URL where it redirects to isn't correctly build. The URL is in the following structure (note the double [root] sections in the URL - this is what is wrong):
http://localhost/[root]/[root]/admin/dashboard
Off course their are errors shown because this controller / action doesn't exists. The URL should be off this form:
http://localhost/[root]/admin/dashboard
I can't seem to find what the exact problem is since cakePHP is not my core dessert, is there anyone that can point me in the right direction?
Thanks!
$this->redirect("YOUR URL");
example $this->redirect('/admins/dashboard');
This way you can redirect easily!
You can use the 'full_base' parameter in Router::url.
e.g.
$url = Router::url( array(
'controller' => 'posts',
'action' => 'view',
'id' => $post[id],
'full' => true
) );
This will give you a full base url. I found this fixed routing problems when I was running CakePHP in a subdirectory of localhost.
you can simply right like this
$this->redirect(array('controller' => 'admins', 'action' => 'dashboard'));
You can simply right this
Router::connect('/', array('controller' => 'users', 'action' => 'index'));
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.