I've got 2 links in my layout.phtml and a route in the bootstrap:
1. Link:
echo $this->url(array('controller' => 'aktuelles', 'action' => 'index'), null, true );
// creates: http://localhost/aktuelles
2: Link
echo $this->url(array('controller' => 'projekte', 'action' => 'wohnen', 'projektId' => 26), 'projekte-galeria', false);
// creates: http://localhost/projekte/wohnen/26
Route:
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$route = new Zend_Controller_Router_Route( 'projekte/wohnen/:projektId',
array(
'module' => 'web',
'controller' => 'projekte',
'action' => 'wohnen',
'projektId' => null)
);
$router->addRoute( 'projekte-galeria', $route);
When I load the page everything is displayed correctly and the urls are all correct.
Problem: As soon as i click on the second link (http://localhost/projekte/wohnen/26), the first link is changing:
from: localhost/aktuelles
to : localhost/projekte/wohnen
Why is the link changed?
Try to force to use the default route: instead of null use 'default' as the second parameter in the first url.
BTW - the part 'controller' => 'projekte', 'action' => 'wohnen' in the second url is redundant, because you predefine these parameters in the route. The second link could by simplified like this:
echo $this->url(array('projektId' => 26), 'projekte-galeria', false);
Have a look at this solution as an alternative way to handle routes Simple rewrites in Zend Framework
Related
I have this following routing. How to tell /nice-looking-url to load /home/www/html/project/public/static/index.html which is not in the Controller
$route = Http\Literal::factory(array(
'route' => '/nice-looking-url',
'defaults' => array(
'controller' => 'Application\Controller\Nicelookingurl',
'action' => 'index'
),
));
$router->addRoute('nicelookingurl', $route, null);
The simple way to do so is to add this line
return $this->renderScript('insert-your-script-path-here');
as first line in your Nicelookingurl index action
If you want to disable the layout you can also add
$this->_helper->layout()->disableLayout();
I am using CakePHP 1.3 and have some troubles with prefix routing.
I configured routes like that:
Router::connect(
'/listing/*',
array(
'controller' => 'dsc_dates',
'action' => 'listing',
)
);
Router::connect(
'/modular/listing/*',
array(
'controller' => 'dsc_dates',
'action' => 'listing',
'prefix' => 'modular'
)
);
in my controller there are two functions:
function modular_listing($order = null,$orderDirection = null, $items=null, $location_id=null) {
$this->layout='module';
$this->setAction('listing',$order, $orderDirection, $items, $location_id);
}
function listing($order = null,$orderDirection = null, $items=null, $location_id=null){...}
The prefix action should just change some things and then operate like the normal 'listing' method. Until here it works fine.
But if i create relative links (with HTML Helper) Router::url() uses 'modular_listing' as action which does not fit into my routes. It should be 'listing' instead of 'modular_listing'.
The controller params are correct with 'listing' as action but the router params still says 'modular_listing'.
So relative links:
$this->Html->link('example',array('parameter'));
will end up in:
/dsc_dates/modular_listing/parameter
How can I get the correct links so that the router uses 'listing' as action?
UPDATE:
It is not an alternative to add 'controller' and 'action' to the url array of the link generation. In fact I have problems with the automatically generated relative links from the paginator.
I couldn't tell if you wanted the generated Html->link() routes with the leading controller or not, so I did both:
Controller (note the renderer):
// DscDatesController.php
public function listing($param = null) {
$this->set('param', $param);
$this->render('listing');
}
public function modular_listing($param = null) {
//
$this->setAction('listing', $param);
}
Routes:
// routes.php
Router::connect(
// notice no leading DS
'listing/*',
array(
'controller' => 'DscDates',
'action' => 'listing'
)
);
Router::connect(
'/modular/listing/*',
array(
'controller' => 'DscDates',
'action' => 'listing'
)
);
View:
// DscDates/listing.ctp
<?php
// generates /dsc_dates/listing/:param
echo $this->Html->link(
'example',
array('controller'=>'dsc_dates', 'action'=>'listing', $param));
// generates /listing/:param
echo $this->Html->link(
'example',
array('action'=>'listing', $param));
About wildcards, DS and routing order:
CakePHP broken index method
HTH :)
I have a problem with routing in my app.
$this->redirect('someone', array('controller' => 'account', 'action' => 'settings'));
It always redirects to app.dev/someone doesn't matter what is in array.
I have found that problem is in method assemble from TreeRouteStack.php
TreeRouteStack.php:347
$path = $this->baseUrl . $route->assemble(array_merge($this->defaultParams, $params), $options);
I tried "die" before this line to test $params - everything is ok. So die(implode(',',$params)); before code above returns
account, settings
But after this echo $path returns
/someone
instead of
/someone/account/settings
Where could be problem? I'm using ZF2 2.2.2
Use
$this->redirect()->toRoute(
'someone',
array('controller' => 'account', 'action' => 'settings')
);
instead of
$this->redirect(
'someone',
array('controller' => 'account', 'action' => 'settings')
);
Please take a look at Redirect Plugin in zf2 docs
I'm using Zend Framework 1.12 and have this route:
$router->addRoute('item_start',
new Zend_Controller_Router_Route_Regex(
'(foo|bar|baz)',
array(
'module' => 'default',
'controller' => 'item',
'action' => 'start'
),
array(
1 => 'area'
),
'%s'
)
);
Problem is, when I call '/foo' and use the Url Helper in the View, it doesn't give me any parameters:
$this->url(array("page"=>1));
// returns '/foo' (expected '/foo/page/1')
$this->url(array("page"=>1), "item_start", true);
// also returns '/foo'
Any idea how to get the page-parameter into the URL? I can't use the wildcard like in the standard route, can't I?
In addition to David's suggestions, you could change this route to use the standard route class, and then keep the wildcard option:
$router->addRoute('item_start',
new Zend_Controller_Router_Route(
':area/*',
array(
'module' => 'default',
'controller' => 'item',
'action' => 'start'
),
array(
'area' => '(foo|bar|baz)'
)
)
);
// in your view:
echo $this->url(array('area' => 'foo', 'page' => 1), 'item_start');
Your Regex route doesn't have a page parameter, so when the url view-helper ends up calling Route::assemble() with the parameters you feed it, it ignores your page value.
The two choices that come to mind are:
Modify your regex to include a (probably optional with default value) page parameter
Manage the page parameter outside of your route in the query string.
Is it possible in CakePHP to have URL aliases in routes.php? Or by what other means can achieve something equivalent:
Lets assume I have some paginated views. Among the possible orderings there are particular ones I want to bind to a simple URL. E.g.:
http://example.com/headlines => http://example.com/posts/listView/page:1/sort:Post.created/direction:desc
http://example.com/hottopics => http://example.com/posts/listView/page:1/sort:Post.view_count/direction:desc etc.
How do I add parameters to a Router::connect()? Pseudo code:
Router::connect('/'.__('headlines',true),
array(
'controller' => 'posts',
'action' => 'listView'
'params' => 'page:1/sort:Post.created/direction:desc',
)
);
Note that the Router "translates" a URL into Controllers, Actions and Params, it doesn't "forward" URLs to other URLs. As such, write it like this:
Router::connect('/headlines',
array(
'controller' => 'posts',
'action' => 'listView'
'page' => 1,
'sort' => 'Post.created',
'direction' => 'desc'
)
);
I don't think '/'.__('headlines', true) would work, since the app is not sufficiently set up at this point to translate anything, so you'd only always get the word in your default language back. Also, you couldn't switch the language anymore after this point, the first use of __() locks the language.
You would need to connect all URLs explictly. To save you some typing, you could do this:
$headlines = array('en' => 'headlines', 'de' => 'schlagzeilen', ...);
foreach ($headlines as $lang => $headline) {
Router::connect("/$headline", array('controller' => ..., 'lang' => $lang));
}
That will create a $this->param['named']['lang'] variable, which you should use in the URL anyway.
Yes, it is possible... Bootstrap.php loads before routes so if you set there something like:
session_start();
if(isset($_SESSION['lng'])){
Configure::write('Config.language', $_SESSION['lng']);
}
...and in your app controller in beforeFilter:
$language = 'xy';
Configure::write('Config.language', $language);
$_SESSION['lng'] = $language;
So initial page render you prompt for language, redirect to xy.site.com or www.site.com/xy whatever you prefer. Now second render will change $language and on page links and set $_SESSION['lang']...
All router links like:
Router::connect(__('/:gender/search/:looking_for/*'), array('controller' => 'users', 'action' => 'search'));
will become:
Router::connect(__('/:gender/trazi/:looking_for/*'), array('controller' => 'users', 'action' => 'search'));
or:
Router::connect(__('/:gender/suche/:looking_for/*'), array('controller' => 'users', 'action' => 'search'));
100% tested, works in CakePHP 2.2. Also further improvement is possible if you put subdomain/language url parser in the bootstrap itself...