在Laravel 4中,我能够使用...
获取当前路线名称Route::currentRouteName()
如何在Laravel 5中完成?
答案 0 :(得分:335)
试试这个
Route::getCurrentRoute()->getPath();
或
\Request::route()->getName()
来自v5。+
use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();
Laravel 5.3
Route::currentRouteName(); //use Illuminate\Support\Facades\Route;
或者如果您需要动作名称
Route::getCurrentRoute()->getActionName();
您可以在Laravel API中找到有关laravel Routes的所有信息: http://laravel.com/api/5.0/Illuminate/Routing/Router.html http://laravel.com/api/5.0/Illuminate/Routing.html
检索请求URI
path方法返回请求的URI。因此,如果传入请求的目标是http://example.com/foo/bar
,则路径方法将返回foo/bar
:
$uri = $request->path();
is
方法允许您验证传入请求URI是否与给定模式匹配。使用此方法时,您可以使用*
字符作为通配符:
if ($request->is('admin/*')) {
//
}
要获取完整的URL,而不仅仅是路径信息,您可以在请求实例上使用url方法:
$url = $request->url();
答案 1 :(得分:32)
使用Laravel 5.1,您可以使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content='width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0' name='viewport'>
<title>European Data Science Academy (EDSA) 2015</title>
<script src='javascripts/d3.js' type='text/javascript'></script>
<script src='javascripts/crossfilter.js' type='text/javascript'></script>
<script src='javascripts/dc.js' type='text/javascript'></script>
<script src='javascripts/bootstrap.min.js' type='text/javascript'></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://cdn.leafletjs.com/leaflet-0.7.1/leaflet.js"></script>
<link href='stylesheets/bootstrap.min.css' rel='stylesheet' type='text/css'>
<link href="css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
<link href='stylesheets/dc.css' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="css/dc.css"/>
.background {
fill: none;
pointer-events: all;
display: block;
margin: auto;
}
.feature {
fill: #ccc;
cursor: pointer;
}
.feature.active {
fill: orange;
}
.mesh {
fill: none;
stroke: #fff;
stroke-linecap: round;
stroke-linejoin: round;
}
</style>
</head>
<body>
<!-- This is the Tittle of the First Container -->
<div class='navbar navbar-fixed-top'>
<div class='navbar-inner'>
<div class='container'>
<a class="brand">European Data Science Academy (EDSA)</a>
<img src="img/edsalogo2.png" alt="European Data Science Academy" width="142" height="142" align="right">
</div>
</div>
</div>
<!-- This is the main frame -->
<div class='container' id='main-container'>
<div class='content'>
<div class='container' style='font: 11px sans-serif;'>
<h3>Visualisation of Numbers of Data Science Jobs</h3>
<h4> Demo</h4>
<!-- This is the frame that contains the Map, Employer, Data Table-->
<div class='row-fluid'>
<div class='remaining-graphs span8'>
<!-- This is the frame that contains the Map-->
<div class='row-fluid'>
<div id="us-chart" align="center">
<!--<div class='bubble-graph span12' id='dc-bubble-graph'> -->
<h4>Distribution of Jobs by Country</h4>
<a class="reset" href="javascript:usChart.filterAll();dc.redrawAll();" style="display: none;">Reset</a>
<!--This is the reset class of the Map -->
<span class="reset" style="display: none;"> | Current filter: <span class="filter"></span></span>
<div class="clearfix"></div>
</div>
</div>
<!-- /other little graphs go here DataTable -->
<div class='row-fluid'>
<div class='span12 table-graph'>
<h4>Data Table for Filtered Countries</h4>
<table class='table table-hover dc-data-table' id='dc-table-graph'>
<thead>
<tr class='header'>
<th>Country</th>
<th>Location</th>
<th>Skills</th>
<th>Jobs</th>
<th>Employer</th>
<!-- <th>Link</th> -->
</tr>
</thead>
</table>
</div>
</div>
</div>
<!-- This is the frame that contains the Location-->
<div class='remaining-graphs span4'>
<div class='row-fluid'>
<div class='row-graph span8' id='dc-row-graph' style='color:black;'>
<h4>Search by Location</h4>
<!-- Reset filters selected in the pie-->
<a class="reset" href="javascript:pieChart2.filterAll();dc.redrawAll();" style="display: none;">Reset</a>
<!--This is the reset class -->
<span class="reset" style="display: none;"> | Current filter: <span class="filter"></span></span>
<div class="clearfix"></div>
</div>
<div class='row-graph span4' id='dc-row-graph2' style='color:black;'>
<h4>Search by Skills</h4>
<!-- Reset filters selected in the pie-->
<a class="reset" href="javascript:pieChart2.filterAll();dc.redrawAll();" style="display: none;">Reset</a>
<!--This is the reset class -->
<span class="reset" style="display: none;"> | Current filter: <span class="filter"></span></span>
<div class="clearfix"></div>
</div>
</div>
</div>
</div> <!-- Ediv class='row-fluid'-->
</div> <!-- END div class='container' style='font:-->
</div> <!-- END div class='content' -->
</div> <!--END div class='container' id='main-container' -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script>
<script type="text/javascript" src="js/d3.js"></script>
<script type="text/javascript" src="js/crossfilter.js"></script>
<script type="text/javascript" src="js/dc.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://cdn.leafletjs.com/leaflet-0.7.1/leaflet.js"></script>
<!-- THIS IS THE MOST IMPORTANT PART -->
<script type="text/javascript">
var width = 590,
height = 500,
active = d3.select(null);
var projection = d3.geo.mercator()
//.center([3.9, 43.0])
.precision(.1)
.scale((1200 + 1) / 2 )
.translate([660 / 4, 3360 / 4]);
var zoom = d3.behavior.zoom()
.translate([0, 0])
.scale(1)
.scaleExtent([1, 8])
.on("zoom", zoomed);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.on("click", stopped, true);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", reset);
var g = svg.append("g");
svg
.call(zoom) // delete this line to disable free zooming
.call(zoom.event);
d3.json("eu.json", function(error, data) {
if (error) throw error;
g.selectAll("path")
.data(topojson.feature(data, data.objects.europe).features)
.enter().append("path")
.attr("d", path)
.attr("class", "feature")
.on("click", clicked);
g.append("path")
.datum(topojson.mesh(data, data.objects.europe, function(a, b) { return a !== b; }))
.attr("class", "mesh")
.attr("d", path);
});
function clicked(d) {
if (active.node() === this) return reset();
active.classed("active", false);
active = d3.select(this).classed("active", true);
var bounds = path.bounds(d),
dx = bounds[1][0] - bounds[0][0],
dy = bounds[1][1] - bounds[0][1],
x = (bounds[0][0] + bounds[1][0]) / 2,
y = (bounds[0][1] + bounds[1][1]) / 2,
scale = .9 / Math.max(dx / width, dy / height),
translate = [width / 2 - scale * x, height / 2 - scale * y];
svg.transition()
.duration(750)
.call(zoom.translate(translate).scale(scale).event);
}
function reset() {
active.classed("active", false);
active = d3.select(null);
svg.transition()
.duration(750)
.call(zoom.translate([0, 0]).scale(1).event);
}
function zoomed() {
g.style("stroke-width", 1.5 / d3.event.scale + "px");
g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
// If the drag behavior prevents the default click,
// also stop propagation so we don’t click-to-zoom.
function stopped() {
if (d3.event.defaultPrevented) d3.event.stopPropagation();
}
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-33628816-1']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>
答案 2 :(得分:23)
找到一种方法来查找当前路线名称适用于laravel v5 , v5.1.28 和 v5.2.10
命名空间
use Illuminate\Support\Facades\Route;
和
$currentPath= Route::getFacadeRoot()->current()->uri();
对于Laravel laravel v5.3 ,您可以使用:
use Illuminate\Support\Facades\Route;
Route::currentRouteName();
答案 3 :(得分:21)
如果您需要网址,而不是路由名称,则无需使用/要求任何其他类:
url()->current();
答案 4 :(得分:17)
如果您想在多条路线上选择菜单,可以这样做:
<li class="{{ (Request::is('products/*') || Request::is('products') || Request::is('product/*') ? 'active' : '') }}"><a href="{{url('products')}}"><i class="fa fa-code-fork"></i> Products</a></li>
或者,如果您只想选择单个菜单,您可以这样做:
<li class="{{ (Request::is('/users') ? 'active' : '') }}"><a href="{{url('/')}}"><i class="fa fa-envelope"></i> Users</a></li>
还在 Laravel 5.2
中进行了测试希望这有助于某人。
答案 5 :(得分:11)
Laravel 5.2你可以使用
$request->route()->getName()
它将为您提供当前路线名称。
答案 6 :(得分:8)
在5.2中,您可以直接使用请求:
$request->route()->getName();
或通过辅助方法:
request()->route()->getName();
输出示例:
"home.index"
答案 7 :(得分:6)
最短的路线是路线外观
\Route::current()->getName()
这也适用于laravel 5.4。*
答案 8 :(得分:5)
您可以在模板中使用:
<?php $path = Route::getCurrentRoute()->getPath(); ?>
<?php if (starts_with($path, 'admin/')) echo "active"; ?>
答案 9 :(得分:4)
现在在Laravel 5.3
我看到你可以尝试同样的方式:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
https://laravel.com/docs/5.3/routing#accessing-the-current-route
答案 10 :(得分:3)
在控制器操作中,您可以这样做:
public function someAction(Request $request)
{
$routeName = $request->route()->getName();
}
$request
这里由Laravel的服务容器解决。
getName()
仅返回named routes的路由名称,否则返回null
(但您仍可以浏览\Illuminate\Routing\Route
对象以获取其他感兴趣的内容。
换句话说,您应该像这样定义您的路线以返回“nameOfMyRoute”:
Route::get('my/some-action', [
'as' => 'nameOfMyRoute',
'uses' => 'MyController@someAction'
]);
答案 11 :(得分:3)
Request::path();
更好,请记住Use Request;
答案 12 :(得分:3)
您可以使用以下代码在刀片文件中获取路由名称
request()->route()->uri
答案 13 :(得分:2)
访问当前路由(v5.3及更高版本)
您可以在Route外观上使用current,currentRouteName和currentRouteAction方法来访问有关处理传入请求的路由的信息:
describe("component methods", () => {
let wrapper;
let props;
let item;
beforeEach(() => {
props = createTestProps();
wrapper = shallow(<MyList {...props} />);
});
describe("renderItem", () => {
describe("user", () => {
beforeEach(() => {
item = {
name: 'Chris Jackson',
avatar_url: 'https://s3.amazonaws.com/uifaces/faces/twitter/adhamdannaway/128.jpg',
subtitle: 'Vice Chairman'
};
});
it("should display the order as a <ListItem />", () => {
expect(
wrapper
.instance()
.renderItem(item)
.find("ListItem")
).toHaveLength(1);
});
});
});
});
请同时参阅Route Facade和Route实例的基础类的API文档,以查看所有可访问的方法。
参考:https://laravel.com/docs/5.2/routing#accessing-the-current-route
答案 14 :(得分:1)
我用于在larvel 5.3中获取路线名称
JavascriptExecutor jse1 = (JavascriptExecutor) driver;
jse1.executeScript("window.scroll(0,250);");
答案 15 :(得分:1)
在控制器中访问当前路由名称
即-http:// localhost /您的项目名称/编辑
$request->segment(1); // edit
(或)
$request->url(); // http://localhost/your_project_name/edit
答案 16 :(得分:1)
访问当前路线
在Blade模板中获取当前路线名称
{{ Route::currentRouteName() }}
有关更多信息,https://laravel.com/docs/5.5/routing#accessing-the-current-route
答案 17 :(得分:1)
查看\Illuminate\Routing\Router.php
,您可以通过在控制器方法中注入路由器来使用方法currentRouteNamed()
。例如:
use Illuminate\Routing\Router;
public function index(Request $request, Router $router) {
return view($router->currentRouteNamed('foo') ? 'view1' : 'view2');
}
或使用Route facade:
public function index(Request $request) {
return view(\Route::currentRouteNamed('foo') ? 'view1' : 'view2');
}
您还可以使用方法is()
来检查路由是否被命名为任何给定参数,但要注意此方法使用preg_match()
并且我经历过它会导致使用虚线路径的奇怪行为名称(如'foo.bar.done'
)。 preg_match()
周围还有表现问题
这是PHP社区的一个重要主题。
public function index(Request $request) {
return view(\Route::is('foo', 'bar') ? 'view1' : 'view2');
}
答案 18 :(得分:0)
由于某些原因,我无法使用任何这些解决方案。因此我只是将web.php
中的路由声明为$router->get('/api/v1/users', ['as' => 'index', 'uses' => 'UserController@index'])
,并在我的控制器中使用$routeName = $request->route()[1]['as'];
获得了路由的名称,其中$request
是\Illuminate\Http\Request $request
中的typehinted参数index
的{{1}}方法
使用流明5.6。希望对别人有帮助。
答案 19 :(得分:0)
解决方案:
$routeArray = app('request')->route()->getAction();
$controllerAction = class_basename($routeArray['controller']);
list($controller, $route) = explode('@', $controllerAction);
echo $route;
答案 20 :(得分:0)
我认为最简单的解决方案是使用此帮助程序:
ListView(
children: <Widget>[
LimitedBox(
maxWidth: 50,
child: Container(
width: 50,
child: TextFormField(
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
hintText: 'you@example.com',
hintStyle: TextStyle(fontSize: 10, color: Colors.white),
labelText: 'E-mail adress',
labelStyle: TextStyle(
color: Colors.white,
)),
),
),
),
有关文档,请参见this link
答案 21 :(得分:0)
您可以使用以下方法:
Route::getCurrentRoute()->getPath();
在Laravel 6.0版中,可以使用以下方法:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
答案 22 :(得分:0)
在帮助文件中,
您可以使用Route::current()->uri()
获取当前网址。
因此,如果您比较路线名称以在菜单上设置活动类,那么使用
会更好 Route::currentRouteName()
获取路线名称并进行比较
答案 23 :(得分:0)
您可以使用以下代码行:url()->current()
在刀片文件中:{{url()->current()}}
答案 24 :(得分:0)
有很多方法可以做到这一点。您可以输入:
\Illuminate\Support\Facades\Request::route()->getName()
获取路线名称。
答案 25 :(得分:0)
如果在直接查看时需要路由名称或 url id 没有人回答 直接查看路线名称
$routeName = Request::route()->getName();
用于查看 url 中的 id
$url_id = Request::segment(2);
答案 26 :(得分:0)
您可以做的第一件事是在 class 的顶部导入 namespace。
use Illuminate\Support\Facades\Route;
laravel v8
$route = Route::current(); // Illuminate\Routing\Route
$name = Route::currentRouteName(); // RouteName
$action = Route::currentRouteAction(); // Action
Laravel v7,6 和 5.8
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();