【发布时间】:2014-04-11 22:05:48
【问题描述】:
我是 laravel 4.1 的新手,正在学习 Jeffry Way 的课程
由于 laravel 3 上的解释,我的代码中有错误,我使用的是 4.1。
Route::get('{shortened}', function($shortend)
{
// query the DB For the row with that short url
$row = Url::where('shortend', '=', $shortend)->first();
// if not found redirect to home page
if (is_null($row) ) return Redirect::to('/');
// else grab the url and redirect
return Redirect::to($row->url);
错误是:
继续循环到主页而不是网站
这是我的完整 routes.php 文件
Route::get('/', function()
{
return View::make('hello');
});
Route::post('/', function()
{
$url = Input::get('url');
$record = Url::where('urls', '=', $url)->first();
if ($record) {
return View::make('result')
->with('shortend', $record->shortend);
}
// otherwise add new row and return shortned url
// create a results view and present the short url to the usr
});
Route::get('/sucess', function ()
{
return View::make('sucess');
});
Route::post('/sucess', function (){
$done = ' Great data inserted ';
$site_data = Input::get('data_insert');
$site = new Url;
$site->urls = $site_data;
$site->shortend = 'masts';
$site->save();
return $done;
});
Route::get('{shortend}', function ($shortend)
{
// query the DB For the row with that short url
$row = Url::where('shortend', '=', $shortend)->first();
// if not found redirect to home page
if (is_null($row) ) return Redirect::to('/');
// else grab the url and redirect
});
Route::get('{url}', function ($url)
{
$row = Url::where('urls', '=', $urls)->first();
return Redirect::to($row->url);
});
问题是当我在 else 部分返回“消息”时,代码运行完美 如果我尝试将其重定向到我从数据库中获取的外部页面(如 site.com) 我有循环。
我尝试为上面的 url 创建一个路由,但我得到的只是一个空白页面。
【问题讨论】:
-
您的其他路线是在这条路线之上还是之下定义的?您应该确保您的其他路由定义在此路由之上,以便它们具有更高的优先级。如果此路由高于其他路由,
'/'将进入此路由处理程序并导致重定向循环。 -
其他路线在我的代码的那部分之上