我的web.php中有两条不同的路线
Route::get('/', 'Web\DashboardController@index');
Route::get('/my-page/{alias}', 'Web\MyPageController@index');
我想将第二页作为我的目标网页而不更改网址。就像访问者访问网站时一样,它会降落到http://www.url.com/my-page/{alias}
答案 0 :(得分:2)
有多种方法可以实现这一目标,其中两种常见方法是:
<?php
session_start();
?>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(document).on("change","#sel",function(){
var val = $(this).val();
console.log(val);
$.ajax({
type: 'post',
url: 'index.php?action=data',
datatype:'json',
data: {"option":val},
success: function (response) {
alert(val);
location.href="index.php";
}
});
});
});
</script>
</head>
<body>
<div id="select_box">
<select id="sel" name="sel">
<option <?php if($_SESSION['val']=="a") echo "selected" ;?> value="a">a</option>
<option <?php if($_SESSION['val']=="b") echo "selected" ;?> value="b">b</option>
<option <?php if($_SESSION['val']=="c") echo "selected" ;?> value="c">c</option>
<option <?php if($_SESSION['val']=="d") echo "selected" ;?> value="d">d</option>
<option <?php if($_SESSION['val']=="e") echo "selected" ;?> value="e">e</option>
<option <?php if($_SESSION['val']=="e") echo "selected" ;?> value="f">f</option>
</select>
</div>
</body>
</html>
<?php
$val = $_POST['option'];
if($_REQUEST['action']=="data"){
$_SESSION['val']=$val;
echo "Selected value = ".$_SESSION['val'];
}elseif ($val=="") {
echo "Selected value = ".$_SESSION['val'];
}
?>
或
Route::get('/', function () {
$alias = ...; // find an alias
return redirect()->route('mypage', ['alias' => $alias]);
});
Route::get('/my-page/{alias}', 'Web\MyPageController@index')->name('mypage');