laravel中的路由跳转方式+前端传参方式总结

最近终于有时间撸博客了 恭喜自己


## laravel路由的基本设定
// ->name() 方法 可以给路由起名字 直接调用
Route::any('save','SiteController@save')->name('save');

1.页面

{{url('/save')}}
{{action('SiteController@save')}}
{{route('save')}}

三种跳转方式,推荐第三种,给路由起别名,命名之后,方法名修改不会影响程序跳转。

<a>中跳转

<a href="{{route('courses')}}?{{query_builder(['category_id', 'scene','page'], ['category_id' => 0,'page'=>1])}}">不限</a>

<a href="{{url('admin/organization/createAuthCodeView', ['id' => $list['id']])}}">   
...
</a>

//后台引用参数时候直接
$request->input('id')


在form表单中提交跳转

<form action="{{route('exam.practice.save')}}">
    <div class=""> 
        <!--判断题-->
        <div class="">
            <input class=""  name="question[{{$question['id']}}]"  id="f{{$question['id']}}1" value="1" type="radio">
            <label for="f{{$question['id']}}1" >
                <i class=""></i>
                <div class=""><p>正确 </p></div>
             </label>
         </div>
         <div class="">
                 <input class=""  name="question[{{$question['id']}}]"  id="f{{$question['id']}}2" value="0" type="radio">
                 <label for="f{{$question['id']}}2">
                     <i class=""></i>
                     <div class=""><p>错误</p></div>
                 </label>
         </div>
    </div>
    <a href="javascript:;" class="" id="submit">提交</a>
</form>

在ajax中跳转(使用时候注意url:‘ xxxx’单引号)

//前端代码
$.ajax({
    type: 'post',
    url: '{{route('credits')}}',
    data: {
          ...
    },
    dataType: 'json',
    headers: {
         'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    success: function (data) {
         ...
   	}
 });


//后台返回代码
return $this->response('0', '兑换成功', []);



2.后端

// 后端跳转 类似前端a链接 跳转
return redirect()->to('/save');
return redirect()->action('SiteController@save');
return redirect()->route('save');
return back();  // 跳转到上一页

同样,第三种命名的方法,路由方法名改变,不影响程序跳转。



(以上资料内容侵删)
转载时请告知(以上资料内容侵删)
每个人都知道的,哪怕已经很糟糕了但是努力生活依旧很重要。
原文地址:https://www.cnblogs.com/ppppian2020/p/15131123.html