简体   繁体   English

将参数传递给Laravel中的控制器

[英]Pass a Parameter to a Controller in Laravel

I would like to pass the id of an object to a controller in laravel . 我想将objectid传递给laravelcontroller Basically I've a page that displays the information relating to a book . 基本上,我有一个页面来显示与book有关的信息。 The user has the option to add themes to that book. 用户可以选择将themes添加到该书。 The line below is the link they click in order to open the create theme form : 下面的行是他们单击以打开创建主题form

<a href="{{action('ThemeController@create')}}" class="btn btn-primary">Add Theme</a>

and the ThemeController method: ThemeController方法:

public function create(){
        $books = Book::all();
        return View::make('theme.create', compact('books'));
    }

Currently this brings up a Form with a list of all of my books in a dropdown. 目前,这会弹出一个表格,其中包含我所有书籍的下拉列表。 I would rather just pass the id of the book the user was on prior to being brought to this form and use that instead. 我宁愿只在传递给该表单之前传递用户所在的书的id ,而是使用它。 Is there any way I can do that? 有什么办法可以做到吗? I've also included my routes.php file: 我还包括了routes.php文件:

Route::model('book', 'Book');
//Show pages
Route::get('/books', 'BookController@index');
Route::get('book/create', 'BookController@create');
Route::get('book/edit/{book}', 'BookController@edit');
Route::get('book/delete/{book}', 'BookController@delete');
Route::get('book/view/{book}', 'BookController@view');
////Handle form submissions
Route::post('book/create', 'BookController@handleCreate');
Route::post('book/edit', 'BookController@handleEdit');
Route::post('book/delete', 'BookController@handleDelete');


//Themes
Route::model('theme', 'Theme');
Route::get('/themes', 'ThemeController@index');
Route::get('theme/create', 'ThemeController@create');
Route::get('theme/edit/{book}', 'ThemeController@edit');
Route::get('theme/delete/{book}', 'ThemeController@delete');
Route::get('theme/view/{book}', 'ThemeController@view');
Route::post('theme/create', 'ThemeController@handleCreate');
Route::post('theme/edit', 'ThemeController@handleEdit');
Route::post('theme/delete', 'ThemeController@handleDelete');

You can still use normal $_GET methodology. 您仍然可以使用常规的$ _GET方法。 On your route: 在您的路线上:
book/create You could simply request. book/create您只需索取即可。 book/create?book_id=xxyy Then in your controller create function book/create?book_id=xxyy然后在您的控制器中创建函数

public function create(){
    $bookId = Input::get('book_id');
    $books = Book::all()->where('book_id' => $bookId);
    return View::make('theme.create', compact('books'));
}

PLEASE NOTE: I haven't used Laravel in a while, don't trust that elequont query :P.. I was just putting a theoretical "where" in there. 请注意:我已经有一段时间没有使用过Laravel了,不要相信那个elequont查询:P ..我只是在这里放一个理论上的“哪里”。

And here http://laravel.com/docs/requests#basic-input , you will note this: 在这里http://laravel.com/docs/requests#basic-input ,您会注意到以下几点:

"You do not need to worry about the HTTP verb used for the request, as input is accessed in the same way for all verbs." “您不必担心用于请求的HTTP动词,因为所有动词都以相同的方式访问输入。”

So whether you're using get, put, post or delete, whatever you wrapped in your header, you will be able to find using Input Class. 因此,无论您使用的是get,put,post还是Delete,无论包在标题中的是什么,您都可以使用Input Class查找。

EDIT 编辑
Just needed to add, that you won't need to modify your routes at all. 只需添加,就根本不需要修改路线。 This is just a Simple GET parameter in a GET request. 这只是GET请求中的简单GET参数。 Just like in any other application. 就像在其他应用程序中一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM