Reputation: 3115
folks. I'm kinda stuck with Routes
there is a search-form in a top-navigation.
<form action="/search/" class="navbar-search pull-right">
<input type="text" name="q" id="search-query" class="search-query span3" placeholder="Go for it...">
</form>
So if I make a request, the URL looks like
fuelphp.dev/search/?q=qwerty
which is fine. With that URL the user should get results for his/her request provided by action_index()
but if the user goes to fuelphp.dev/search/
(without any parameters), he/she should see the "advanced Search-form" provided by action_advanced()
I alredy created a Controller_Search
classes/controller/search.php (Controller)
class Controller_Search extends Controller_Hybrid
{
public function action_index() {
$this->template->title = 'Results for » '.Input::get('q');
$this->template->content = View::forge('search/index.twig');
}
public function action_advanced()
{
$this->template->title = 'Search » Advanced Search';
$this->template->content = View::forge('search/index.twig', array(
'advancedForm' => true
));
}
}
views/search/index.twig (View)
{% if advancedForm %}
{% include 'advancedSearchForm.twig' %}
{% endif %}
<h4>Hello Template nesting :) </h4>
the problem is - I don't know how to (re)write routes for that.
if I add 'search' => 'search/advanced'
to the routes.php it doesnt work correctly.
Requestin fuelphp.dev/search/?q=qwerty
triggers action_advanced()
of Controller_Search
too, whereas it should trigger action_index()
How should I rewrite my routes (or maybe Controller logic) to get this working ?
UPDATE:
The solutuon was found! No routes configuration needed!
public function action_index() {
if (Input::get('q')) {
$viewTitle = 'Results for » '.Input::get('q');
$viewData = null;
}else{
$viewTitle = 'Search » Advanced Search';
$viewData = Array('advancedForm' => true);
}
$this->template->title = $viewTitle;
$this->template->content = View::forge('search/index.twig', $viewData);
}
But if one of you have a "nicer" way, I'll be glad to see it
Upvotes: 2
Views: 799
Reputation: 5641
Try add 2 routes one of them with a named parameter:
'search' => 'search/advanced',
'search/:q' => 'search/index/'
So when it has some parameter goes to search/index
with the given parameter, but if you don't have parameters it routes to search/advanced
Then on search/index
you can get the search paramer (q
) with $this->param('q')
Upvotes: 1