-1

我刚开始学习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 设置。

你能帮他修好吗?

4

1 回答 1

0

将路线声明从'(:any)'

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

到 ( '/(:any)')

Route::get('/(:any)', function($shortened){ //... });
于 2013-10-22T01:51:08.330 回答