Laravel 重定向到 post 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23444381/
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 redirect to post method
提问by jeremy castelli
To stay basic I would like to create a bookmark app
为了保持基本,我想创建一个书签应用程序
I have a simple bookmarklet
我有一个简单的书签
javascript:location.href='http://zas.dev/add?url='+encodeURIComponent(location.href)
I created a rest controller
我创建了一个休息控制器
<?php
use zas\Repositories\DbLinkRepository;
class LinksController extends BaseController {
protected $link;
function __construct(DbLinkRepository $link) {
$this->link=$link;
// ...
//$this->beforeFilter('auth.basic', array('except' => array('index', 'show', 'store')));
// ...
}
public function index()
{
//return Redirect::to('home');
}
public function create()
{
}
public function store()
{
return 'hello';
//$this->link->addLink(Input::get('url'));
//return Redirect::to(Input::get('url'));
}
public function show($id)
{
//$url = $this->link->getUrl($id);
//return Redirect::to($url);
}
public function edit($id)
{
}
public function update($id){
}
public function destroy($id){
}
}
in the routes.php, I created a ressource
在routes.php中,我创建了一个资源
Route::resource('links','LinksController');
and as I want to redirect /add to the store method I added
并且因为我想将 /add 重定向到我添加的 store 方法
Route::get('/add',function(){
return Redirect::action('LinksController@store');
});
but it never display the hello message, in place it redirects me to
但它从不显示 hello 消息,而是将我重定向到
I also tried with
我也试过
return Redirect::route('links.store');
without much success
没有多大成功
thanks for your help
感谢您的帮助
采纳答案by sidneydobber
Ok I now get what you are trying to do. This will work:
好的,我现在明白你想要做什么了。这将起作用:
Route::get('add', 'LinksController@store');
Remove:
消除:
Route::resource('links','LinksController');
and remove:
并删除:
Route::get('/add',function(){
return Redirect::action('LinksController@store');
});
Sorry it took so long!
抱歉拖了这么久!
回答by jeremy castelli
The problem is that once you Redirect::
, you loose all the Input
values, so you should manually give them to your controller when you do the redirect, like so :
问题是,一旦你Redirect::
,你失去了所有的Input
值,所以你应该在执行重定向时手动将它们提供给你的控制器,如下所示:
Redirect::route('links.store', ["url" => Input::get("url")]);
Finally add an $url
parameter to your store
method to receive the value we give it in the previous method, like this :
最后$url
向您的store
方法添加一个参数以接收我们在前一个方法中给它的值,如下所示:
public function store($url) {
$this->link->addLink($url);
return Redirect::to($url);
}