如何调用函数
storeSessionData($ name,$ value)@sessionController
来自
partList.blade
用ajax?
我很困惑,如果我有1个模板的多个网址,我应该使用什么路由来调用ajax函数。也许有人会这么善良并给我举个例子怎么做?
SessionController
public function storeSessionData( $name , $value){
switch ($name) {
case 'cpu':
\Session::put($name,$value);
break;
case 'cpu_cooler':
\Session::put($name,$value);
break;
case 'case':
\Session::put($name,$value);
break;
case 'motherboard':
\Session::put($name,$value);
break;
case 'psu':
\Session::put($name,$value);
break;
case 'memory':
\Session::push($name,$value);
break;
case 'sound_card':
\Session::push($name,$value);
break;
case 'wireless_card':
\Session::push($name,$value);
break;
case 'wired_card':
\Session::push($name,$value);
break;
case 'storage':
\Session::push($name,$value);
break;
case 'optical_drive':
\Session::push($name,$value);
break;
case 'gpu':
\Session::push($name,$value);
break;
case 'case_fan':
\Session::push($name,$value);
break;
}
return redirect()->route('compatibilityCheck');
}
Partlist.Blade
@foreach ($parts as $part=>$value)
@foreach ($value as $desc => $val)
<th><a href="?sort={{$desc}}&order=asc">{{trans('specifications.' .$desc)}}</a></th>
@endforeach
@break
@endforeach
</thead>
<tbody>
@foreach ($parts as $part => $value)
<tr>
@foreach ($value as $desc => $val)
@if($val == reset($value))
<td class="part-link" ><a href="{{$slug}}{{$value->slug}}">{{$val}} </a></td>
@elseif ($val == end($value))
<td><a class="button button-primary" href="" // Call ajax function on click {{$session_name}} {{$value->slug}} >Pievienot </a></td>
@else
<td data-title="{{trans('specifications.' .$desc)}}:">{{$val}}</td>
@endif
@endforeach
</tr>
@endforeach
部分列表控制器
class partListController extends Controller
{
public function showAllCpu(Request $request){
// cpu query
$parts = // part query
$parts = $parts->paginate(30);
$data = [
'title' => '',
'parts' => $parts,
'slug' => 'procesors-',
'bgImg' => 'cpubg',
'headTitle' => 'Intel un AMD Procesori',
'headParagraph' => 'Izvēlies jaunākos un izcilākos procesorus',
'session_name' => 'cpu',
];
if ($parts->currentPage() <= $parts->lastpage()) {
return view('partlist', $data);
} else{
abort(404, 'Lapa nēeksistē');
}
}
public function showAllMemory(Request $request){
// part query
$parts = // part query
$parts = $parts->paginate(30);
$data = [
'title' => 'Izvēlies operatīvo atmiņu',
'parts' => $parts,
'slug' => 'operativa-atmina-',
'bgImg' => 'memorybg',
'headTitle' => 'Operatīvā atmiņa',
'headParagraph' => 'Paātrini sava datora ātrdarbību',
'session_name' => 'memory',
];
if ($parts->currentPage() <= $parts->lastpage()) {
return view('partlist', $data);
} else{
abort(404, 'Lapa nēeksistē');
}
}
答案 0 :(得分:1)
这是一个简单的例子:
<强> routes.php文件强>
Route::post('sessions/{name}/{value}', ['as' => 'session.store', 'uses' => 'SessionController@storeSessionData']);
<强> SessionController.php 强>
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class SessionController extends Controller
{
public function storeSessionData($name, $value) {
// Do something with $name and $value
return ['name' => $name, 'value' => $value];
}
}
<强> partlist.blade.php 强>
<html>
<head>
...
<meta name="csrf-token" content="{{ csrf_token() }}" />
...
</head>
...
<td><a class="button button-primary save-session" data-url="{{ route('session.store', ['name' => $session_name, 'value' => $value->slug]) }}">Pievienot</a></td>
...
</html>
<强>的Javascript 强>
$('.save-session').on('click', function(){
var token = $('meta[name="csrf-token"]').attr('content'),
$this = $(this),
url = $this.data('url');
$.post(url, { _token: token }).done(function(msg) {
alert(msg);
});
});