伙计们。我有点卡在路线上
顶部导航中有一个搜索表单。
<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>
所以如果我提出请求,URL 看起来像
fuelphp.dev/search/?q=qwerty
这很好。使用该 URL,用户应该获得由他/她提供的请求的结果action_index()
但是如果用户去fuelphp.dev/search/
(没有任何参数),他/她应该看到由提供的“高级搜索表单”action_advanced()
我已经创建了一个 Controller_Search
类/控制器/search.php(控制器)
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
));
}
}
视图/搜索/index.twig(视图)
{% if advancedForm %}
{% include 'advancedSearchForm.twig' %}
{% endif %}
<h4>Hello Template nesting :) </h4>
问题是 - 我不知道如何(重新)为此编写路线。
如果我添加'search' => 'search/advanced'
到 routes.php 它不能正常工作。
Requestin也fuelphp.dev/search/?q=qwerty
触发action_advanced()
了Controller_Search
,而它应该触发action_index()
我应该如何重写我的路由(或者可能是控制器逻辑)以使其正常工作?
更新:
解决方案找到了!无需路由配置!
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);
}
但是如果你们中的一个人有一个“更好”的方式,我会很高兴看到它