Php laravel 4.1重定向::到()

Php laravel 4.1重定向::到(),php,laravel,Php,Laravel,我是新来的拉威尔4.1和研究杰弗里方式的课程 由于Laravel3的解释,我的代码中有一个错误,我使用的是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 redirec

我是新来的拉威尔4.1和研究杰弗里方式的课程

由于Laravel3的解释,我的代码中有一个错误,我使用的是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部分返回“message”时,代码运行得很好 若我试图将它重定向到外部页面,比如我从数据库获取的site.com 我有一个回路

我试图为上面的url创建一个路由,但我得到的只是一个空白页面

  • DB::where('shortend',$shortend)
    是的缩写
    DB::where('shortend','=',$shortend)

  • Route::get({path?},
    捕获
    '/'
    ,并且
    $shortend
    等于null,因此查询将返回null,因此它将再次重定向到自身

  • 尝试对url参数和函数使用相同的名称

    路由::get({slug}),函数($slug){ //您的控制器方法。 }

  • 从您的Routes文件提交我创建了一个或多或少可以工作的文件:

    Route::get('/', function()
    {
        return View::make('hello'); 
    });
    
    Route::post('/', function (){
        $site = new Url;
        $site->urls = Input::get('url');
        $site->shortened = Str::random(5);
        $site->save();
        return View::make('success')->withSite($site);
    });
    
    Route::get('{shortened}', 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('/');
        // else grab the url and redirect
    
        return Redirect::to($row->url); 
    });
    

    您的其他路由是在该路由之上还是之下定义的?您应该确保您的其他路由是在该路由之上定义的,以便它们具有更高的优先级。如果此路由高于其他路由,
    '/'
    将在该路由处理程序内着陆,并将导致重定向循环。其他路由位于我的代码的该部分之上在我的代码中每件事都很好,比如这个Route::get({path?}),function($shortend){//查询数据库中具有该短url的行$row=url::where('shortend','=',$shortend)->first();//如果没有找到重定向到主页,如果(is_null($row))返回redirect::to('/'));//否则抓取url并重定向返回'Done';但是当我把这个返回重定向::to($row->url)放进去时,我得到了循环,甚至当从DB绘制的url仍然是循环时,您也进入了循环,因为您输入了
    Route::get('{path?}“
    再一次,您必须为从数据库中获取的
    url
    定义路径。我会尝试一下,现在感谢您的快速帮助和帮助。我已经添加了完整的路径供大家审阅。我希望这能解决我的错误并让我理解