Laravel GEOIP 的中间件抛出错误 ->类 Torann\GeoIP\Location 的对象无法转换为字符串



在Laravel中间件上工作,包括使用Torann Geoip检测Geoip检测并将值保存到cookie中以供以后使用,我发现了以下错误:

errorexception in Geolocation.php行21:类的对象 Torann Geoip 位置无法转换为字符串

其中21 -> if($request->$location == null){...

namespace AppHttpMiddleware;
use SymfonyComponentHttpKernelExceptionHttpException;
use Closure;
use GeoIP;
class Geolocation
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Test to see if the requesters have an ip address.
        $location = GeoIP::getLocation();
        if($request->$location == null){
           Cookie::queue('price_geo', 'US','3600');
        }
        Cookie::queue('price_geo', $location->getAttribute('iso_code'),'3600');
        return $next($request);
    }
}

我是Laravel中的中间件:

  1. 这是使用它的正确方法吗?
  2. 此外,我在视图中测试了geoip()->getLocation()并返回该值,任何想法为什么在中间件geolocation.php抛出错误?

$此代码中的许多东西...现在这是您要实现的目标...

  public function handle($request, Closure $next)
    {
        // Test to see if the requesters have an ip address.
        $location = GeoIP::getLocation($request->ip());
        if(isset($location){
           Cookie::queue('price_geo', $location->getAttribute('iso_code'),'3600');
        } else {
           Cookie::queue('price_geo', 'US','3600');
        }

        return $next($request);
    }

最新更新