【问题标题】:Why is this image tracker not working? Laravel为什么这个图像跟踪器不起作用?拉拉维尔
【发布时间】:2016-04-26 21:35:35
【问题描述】:

我在我的 Laravel 5.2 应用中为电子邮件链接创建了一个小型点击跟踪器。

我在我的电子邮件视图中使用这样的字符串;

a href="http://example.com/tracker/1/{{ $slug }}"

然后在我的控制器中,我通过跟踪器 ID(在上述情况下为 1)查找所需的 URL。然后我使用 URL 和用户创建或更新跟踪记录。最后,我重定向到请求的 URL。没有真正的魔法,就像我预期的那样工作。

    public function track($id, $email){
    $track = Track::where('url', $id)->where('user', $email)->first();
    $url = TrackLink::where('id', $id)->first();

    if ($track){
        $track->count += 1;
        $track->save();
    } else {
        $track = new Track;
        $track->count = 1;
        $track->url = $id;
        $track->user = $email;
        $track->save();
    }

    if ($url->type == 'user'){
        return redirect ($url->url.'/'.$email); //in cases where I want to append the email to the url
    }else{
        if ($url->type == 'link') {
            return redirect ($url->url); //in cases where I just need to redirect
        }else{
            return redirect ($url->url); //this is the third case for images
        }
    }

}

但是,我正在尝试使用 1x1 像素 png 来跟踪打开情况。因此,我对 img src 使用了类似的跟踪字符串。该部分的工作原理是它确实调用了控制器中的方法并写入了跟踪记录。但是,图像不会显示。

我试过了;

return ($url->url);

return redirect ($url->url);

两者都没有获取要呈现的图像文件。我错过了什么?

谢谢!

【问题讨论】:

  • 欢迎使用 stackoverflow。你不应该用答案来改变你的问题。您必须发布自己的答案。
  • 很公平。只是不想看起来像回答我问的问题的 dbag!

标签: php laravel


【解决方案1】:

已解决

问题在于 if/else。删除了额外的条件,然后可以访问并正确执行重定向。

现在看起来像这样;

    if ($url->type == 'user'){
        return redirect ($url->url.'/'.$email);
    }else{
        return redirect ($url->url);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 2017-09-02
    • 2016-03-19
    • 1970-01-01
    相关资源
    最近更新 更多