【问题标题】:laravel 4.1 Redirect::to()laravel 4.1 重定向::to()
【发布时间】: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 创建一个路由,但我得到的只是一个空白页面。

【问题讨论】:

  • 您的其他路线是在这条路线之上还是之下定义的?您应该确保您的其他路由定义在此路由之上,以便它们具有更高的优先级。如果此路由高于其他路由,'/' 将进入此路由处理程序并导致重定向循环。
  • 其他路线在我的代码的那部分之上

标签: php laravel


【解决方案1】:
  1. DB::where('shortend', $shortend) 简称 DB::where('shortend', '=', $shortend)

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

  3. 尽量为url参数和函数使用相同的名称。

    Route::get('{slug}', function ($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(); // 如果没有找到重定向到主页 if (is_null($row) ) return Redirect::to('/' ); // 否则抓取 url 并重定向 return 'Done'; 但是当我把这个返回 Redirect::to($row->url); 我得到了循环,即使从 DB 提取的 url 仍然循环跨度>
  • 你进入循环是因为你再次输入Route::get('{path?}'
  • 你必须为url定义你从数据库中获取的路由
  • 我会试试的,现在感谢您的快速帮助和帮助
  • 我已经添加了完整的路线供你们审查,希望能解决我的错误并让我理解
猜你喜欢
  • 2014-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 2014-01-16
  • 1970-01-01
  • 2014-12-22
相关资源
最近更新 更多