【问题标题】:Routing query strings路由查询字符串
【发布时间】:2015-04-12 12:25:43
【问题描述】:

我的模板文件中有一个表单:

                {{ Form::open(array('route' => 'get.index', 'method' => 'get')) }}
                    {{ Form::label('order', 'Order by') }}
                    {{ Form::select('order' , array('firstname' => 'First Name', 'lastname' => 'Last Name', 'state' => 'State')) }}
                    {{ Form::submit('Order results') }}
                {{ Form::close() }}

提交后,此表单会输出一个名为 order 的 GET 查询变量,因此我的 URL 将是:

http://my.app/?order=firstname

这是 get.index 路由:

Route::get('/', array('uses' => 'HomeController@index', 'as' => 'get.index'));

有没有一种简单的方法可以将上面的 URL 更改为类似 http://my.app/order/fistname/?

【问题讨论】:

    标签: php laravel routing url-routing


    【解决方案1】:

    拥有这样一条路线的唯一方法是使用route parameter

    Route::get('/order/{type}', array(
        'uses' => 'HomeController@index', 
        'as' => 'get.index'
    ));
    

    然后您可以在控制器方法中使用type 参数:

    public function index($type) {
    
    }
    

    但它不适用于您的实际表单,除非您在 javascript 中格式化表单操作值以适应此路线。

    但是,如果您可以使用表单以外的其他内容,则可以使用 HTML a 标记轻松完成,例如:

    Order by:
    <a href="{{ route('get.index', 'firstname') }}">firstname</a>
    <a href="{{ route('get.index', 'lastname') }}">lastname</a>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-01
      • 2018-10-24
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多