【问题标题】:laravel 3 - (:any) route not workinglaravel 3 - (:任何)路线不起作用
【发布时间】:2013-10-30 16:35:34
【问题描述】:

我刚刚开始学习 laravel。并从 nettuts+ ( url shortner ) 找到了一个小示例项目。它运作良好,但我面临的唯一问题是 (:any) 路线不起作用。 这是我在文件中的三个路线。

Route::get('/', function()
{
    return View::make('home.index');
});

Route::post('/', function()
{
    $url = Input::get('url');

    // Validate the url
    $v = Url::validate(array('url' => $url));
    if ( $v !== true ) {
        return Redirect::to('/')->with_errors($v->errors);
    }

    // If the url is already in the table, return it
    $record = Url::where_url($url)->first();
    if ( $record ) {
        return View::make('home.result')
                ->with('shortened', $record->shortened);
    }

    // Otherwise, add a new row, and return the shortened url
    $row = Url::create(array(
        'url' => $url,
        'shortened' => Url::get_unique_short_url()
    ));

    // Create a results view, and present the short url to the user
    if ( $row ) {
        return View::make('home.result')->with('shortened', $row->shortened);
    }
});

Route::get('(:any)', function($shortened)
{
    // query the DB for the row with that short url
    $row = Url::where_shortened($shortened)->first();

    // if not found, redirect to home page
    if ( is_null($row) ) return Redirect::to('/');

    // Otherwise, fetch the URL, and redirect.
    return Redirect::to($row->url);
});

前两条路线工作正常,但第三条路线永远不会被激活。它只有在我用 url 中的 index.php 调用它时才有效。就像 /index.php/abc 一样,它也应该适用于 /abc。仅供参考,我也从应用程序配置文件中删除了 index.php 设置。

你能帮他修一下吗?

【问题讨论】:

    标签: laravel laravel-3 laravel-routing


    【解决方案1】:

    '(:any)'更改路由声明

    Route::get('(:any)', function($shortened){ //... });
    

    到 ('/(:any)')

    Route::get('/(:any)', function($shortened){ //... });
    

    【讨论】:

      猜你喜欢
      • 2017-10-02
      • 2013-05-03
      • 2014-09-08
      • 2015-03-18
      • 2017-12-13
      相关资源
      最近更新 更多