return redirect()->route('new-route'); 在上述示例中,将根据名为new-route的命名路由生成URL,并将请求重定向到该URL。 对于Laravel中重定向的应用场景,常见的例子包括: 重定向旧URL到新URL:当网站进行URL结构更改或页面迁移时,可以使用重定向将旧URL重定向到新URL,以确保旧URL的
$url=URL::to('foo'); 路由参数 Route::get('user/{id}',function($id) { return'User '.$id; }); 可选路由参数 Route::get('user/{name?}',function($name=null) { return$name; }); 带有默认值的可选路由参数 Route::get('user/{name?}',function($name='John') ...
Route::get('foo', array('https', function() { return 'Must be over HTTPS'; })); 经常您需要根据路由产生 URLs,您可以通过使用 URL::to 方法: 代码如下: $url = URL::to('foo'); 路由参数 代码如下: Route::get('user/{id}', function($id) { return 'User '.$id; }); 可选的路由...
这种方式的使用更为灵活,因为我们将来要修改 URL 结构,只需要修改 routes.php 中路由的配置就行了。 //For a route with the following URI: profile/{id}returnredirect()->route('profile', [1]); 如果路由中有参数,可以将其作为第二个参数传递到route方法。 returnredirect()->route('profile', [$user...
总结: 在 Laravel 中重定向到具有路由参数的子域,可以通过定义相应的路由和控制器方法来实现。使用route()函数生成带有路由参数的子域 URL,并使用redirect()函数进行重定向操作。如果需要传递其他参数,可以将它们作为第二个参数传递给redirect()函数。
php 文件定义路由开始的。可以通过在浏览器中输入定义的路由 URL 来访问 routes/web.php 中定义的路由。例如,你可以在浏览器中输入 http://example.com/user 来访问以下路由:use App\Http\Controllers\UserController; Route::get('/user', [UserController::class, 'index']);...
在原始的 PHP 中,如果我们需要跳转链接,一般使用的是 header() 方法,并在参数里使用 Location:url 这种方式。在 Laravel 中,可以比较方便地在路由中实现跳转。 Route::get('/get/request/{id}/{name?}', function($id, $name=''){ return 'get:' . $id . ', ' . $name; })->name('get/reques...
If your controller route requires parameters, you may pass them as the second argument to theactionmethod: returnredirect()->action('UserController@profile',['id'=>1]); Redirecting With Flashed Session Data# Redirecting to a new URL andflashing data to the sessionare usually done at the same...
$url = URL::to('foo'); 路由参数 代码如下: Route::get('user/{id}', function($id) { return 'User '.$id; }); 可选路由参数 代码如下: Route::get('user/{name?}', function($name = null) { return $name; }); 带有默认值的可选路由参数 ...
1$url=URL::route('profile'); 2 3$redirect=Redirect::route('profile'); You may access the name of a route that is running via thecurrentRouteNamemethod: 1$name=Route::currentRouteName(); Route Groups Sometimes you may need to apply filters to a group of routes. Instead of specifying ...