我刚开始学习laravel。并从nettuts +(url shortner)找到了一个小样本项目。它运作良好,但只有我面临的问题是(:任何)路线不起作用。 以下是我在档案中的三条路线。
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设置。
你能帮忙解决一下吗?
答案 0 :(得分:0)
从'(:any)'
Route::get('(:any)', function($shortened){ //... });
到('/(:any)'
)
Route::get('/(:any)', function($shortened){ //... });