Работая над промежуточным программным обеспечением Laravel, чтобы включить обнаружение GEOIP с помощью Torann \ GeoIP и сохранить значение в cookie для дальнейшего использования, я обнаружил следующую ошибку:
ErrorException в строке 21 Geolocation.php: Объект класса
Torann \ GeoIP \ Location не может быть преобразован в строку
где строка 21 -> if($request->$location == null){...
namespace App\Http\Middleware;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Closure;
use GeoIP;
class Geolocation
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $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:
geoip()->getLocation()
в поле зрения и возвращает значение, есть идеи, почему в middleware 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);
}
Других решений пока нет …