laravel get routing information

In laravel, we often need to get the current url, but each one is different, and sometimes the requirements we need to get are different, so I will summarize it here for future query and use.

Using the URL class:

// 返回当前页面的地址:http://a.com/platforms
URL::full();
url()->full();

// 返回当前页面的完整路径:http://a.com/platforms
URL::current();
url()->current();

// 返回前一个页面的地址:http://a.com
URL::previous();
url()->previous();
// https://jiahe.com/css/foo.css
URL::secureAsset('css/foo.css');

Using the Request class

// 返回当前页面的完整路径url: http://xx.com/aa/bb
Request::url();
$request->url()

// 路径: /aa/bb
Request::path();
$request->path();

// 获取请求 Uri: /aa/bb/?c=d
Request::getRequestUri();
$request->getRequestUri();

// 获取 Uri: http://xx.com/aa/bb/?c=d
Request::getUri();
$request->getUri();

Get basic routing information

$request->route()->getAction();

Using the Input class

Input::url();
使用$_SERVER获取基础路由
// path:/platforms?a=1
$_SERVER['REQUEST_URI']
// 获取当前基础路由,比如http://a.com/test/a返回http://a.com
$_SERVER["HTTP_HOST"]
// 更多参数可以直接打印SERVER
dd($_SERVER);

Get $ _SERVER (HTTP) information
in laravel laravel can also get the same function as $ _SERVER , we can use the following function to return it as an array

Request::server(); // 可以获取到所有 $_SERVER 信息
$request->server();
Request::server('HTTP_HOST'); // 可以获取到 $_SERVER 中的 HTTP_HOST 信息 (即访问域名)

This article is reproduced from: https://phpartisan.cn/news/58.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324765856&siteId=291194637