Cakephp URL Slugs Question
my urls are like
example.com/deals/by_city/glasgow
I have an action "by_city" in a cities_controller that needs to grab the "glasgow" part of url and do stuff how can i do this with cakephp? I only want to know how to grab this part of the url and store it in variable.
All help appreciated :)
Thanks Dave
Cities Controller
function city_changer($CitySlug = null) {
$active = $this->City->find('first', array('conditions' => array('City.slug' => $CitySlug)));
$cities = $this->City->find('all', array('order' => array('City.name', 'City.name ASC')));
$this->set(compact('active', 'cities'));
}
City changer.ctp
<h2>Daily Deals from <?php echo $active['City']['name'];?></h2>
<form>
<select name="file" size="1" onchange="loadPage(this.form.elements[0])" target="_parent._top" onmouseclick="this.focus()">
<?php foreach($cities as $city) {?>
<option value="/new_avc/deals/by_city/<?ph开发者_运维知识库p echo $city['City']['slug'];?>"><?php echo $city['City']['name'];?></option>
<?php }?>
</select>
</form>
default.ctp
<div id="header_wrapper">
<div id="navigation">
<div id="logo">
<h1>Amazing Voucher Codes</h1>
</div>
<div id="city_nav">
<?php echo $this->requestAction('/cities/city_changer/'.$city['City']['slug'], array('return'));?>
</div>
As there is no database linked to default.ctp having problem getting the slug to pass to the controller :)
Assuming no custom router rules intervene, the third parameter in the URL (after controller/action) is the first parameter passed to the action.
class CitiesController extends AppController {
function by_city( $cityName ) {
// $cityName == 'glascow'
}
}
You should just be able to add a $cityname
parameter to the by_city
method in the deals
controller: this will automatically receive the city name (glasgow from the url above).
精彩评论