定义2条路由
~~~
Route::get('index','IndexController@index')->name('index');
Route::get('admin','IndexController@admin')->name('admin');
~~~
\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php助手函数redirect方法可以实现页面的重定向功能。
~~~
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests as Requests;
class IndexController extends Controller
{
public function index(){
1.// return redirect()->action('IndexController@admin');
2. // return redirect()->route('admin');
3. // return redirect('admin');
}
public function admin(){
return 'admin';
}
}
~~~
1.直接指向控制器,进行重定向
~~~
echo action('IndexController@admin');
~~~
输出结果是:
`http://127.0.0.1/index.php/admin`
2.获取路由别名,进行重定向
~~~
echo route('admin');
~~~
3.根据助手函数携带参数进行重定向
~~~
if (! function_exists('redirect')) {
/**
* Get an instance of the redirector.
*
* @param string|null $to
* @param int $status
* @param array $headers
* @param bool $secure
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
*/
function redirect($to = null, $status = 302, $headers = [], $secure = null)
{
if (is_null($to)) {
return app('redirect');
}
return app('redirect')->to($to, $status, $headers, $secure);
}
}
~~~