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();  // 跳轉到上一頁

同樣,第三種命名的方法,路由方法名改變,不影響程序跳轉。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM