Laravel:如何隐藏 url 参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39951509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel : How to hide url parameter?
提问by Hola
Here the scenario is I want to pass a variable which will be send from one page to another and in next page it's gonna store through a form. So I have passed the variable from first page to second page through the URL. But I want to hide the parameter in the URL. How do I do it?
这里的场景是我想传递一个变量,该变量将从一个页面发送到另一个页面,并在下一页中通过表单存储。所以我已经通过 URL 将变量从第一页传递到第二页。但我想隐藏 URL 中的参数。我该怎么做?
Here is my route :
这是我的路线:
Route::get('/registration/{course_id}',[
'uses'=>'AppController@getregistration',
'as'=>'registration'
]);
And Controller :
和控制器:
public function getregistration($course_id)
{
return view('index')->with('course_id',$course_id);
}
And first page this is how I send the value to first page:
第一页这是我将值发送到第一页的方式:
<li> <a href="{{route('registration',['course_id' => '1'])}}">A</a> </li>
回答by Imran Hossain
Post Method
邮寄方式
Route
路线
Route::post('/registration',['uses'=>'AppController@getregistration','as'=>'registration']);
View
看法
{!!Form::open(array('url' => '/registration')) !!}
{!! Form::hidden('course_id', '1') !!}
{!! Form::submit('registration') !!}
{!! Form::close() !!}
Controller
控制器
public function getregistration(Request $request)
{
$course_id = $request->input('course_id');
return view('index')->with('course_id',$course_id);
}
Get method
获取方法
use encryption method, it will show encrypted id in url
使用加密方法,它将在 url 中显示加密的 id
View
看法
<li> <a href="{{route('registration',['course_id' => Crypt::encrypt('1') ])}}">A</a> </li>
Controller
控制器
public function getregistration($course_id)
{
$course_id = Crypt::decrypt($course_id);
return view('index')->with('course_id',$course_id);
}
回答by Jamal Abdul Nasir
You cannot hide a parameter in URL
. If you don't want to show the ID
then try using SLUG
. I hope you understand what is a SLUG
. If you don't then here it is. If you course title is My new Course Titlethen its slug would be my-new-course-title. And make sure it is uniquelike an ID
in the table. It is also good for SEO, readable and looks good.
您不能在URL
. 如果您不想显示,ID
则尝试使用SLUG
. 我希望你明白什么是SLUG
. 如果你没有,那么它就在这里。如果您的课程标题是我的新课程标题,那么它的 slug 将是my-new-course-title。并确保它是独一无二的,就像ID
表中的一样。它也有利于 SEO,可读性强,看起来不错。
回答by Xain Bin Kafeel
here is no way you hide parameter in url, rather then you convert parameter value encrypt or hash is up to you,
您无法在 url 中隐藏参数,而是您转换参数值 encrypt 或 hash 由您决定,
other-way is save value in session first, then call the value from session without define parameter in url.
另一种方式是先在会话中保存值,然后从会话中调用该值,而无需在 url 中定义参数。
because laravel route only working to pattern of url /string /id, post get. dynamic value you must be writing / getting using pattern method.
因为 Laravel 路由只适用于 url /string /id 的模式,post get。您必须使用模式方法编写/获取动态值。
Thanks.
谢谢。