[英]Zend framework how can i get value from view and controller when render() is used?
[英]How can I get a value from the URL of a view and use it in controller?
我有 2 个 Blade 视图。 在第一个中,我发送了一个product
的id
并重定向到另一个view
,在那里我需要输入其他东西。 在控制器中,我无法从第一个视图中获取id
,以便使用它在我的数据库中存储来自两个视图的所有信息。
我需要得到shoe_id/$product->id
值!
这是第一个视图:
<form action="{{ route('search') }}" method="GET">
<input type="text" name="search" required/>
<button class ="btn" type="submit">Search</button>
</form>
</h3>
@if($products->isNotEmpty())
@foreach ($products as $product)
<div class="col-md-2">
<form action="sell/{{$product->id}}" method="POST">
@csrf
{{-- <a href="{{'products/sell/' . $product->id }}"> --}}
<button name="shoe_id" value="{{ $product->id }}" class="btn">
<img src="/imagini/{{ $product->id }}.1.jpg" style="height: 210px">
</button>
{{-- </a> --}}
</form>
<p style="text-align: center">{{ $product->title }}</p>
</div>
@endforeach
这是第二种观点:
<form action="/select/{product_id}" method="post">
@csrf
<h2>Select a size:</h2>
<div class="row" style="margin-bottom: 50px">
<select class="select2" name="size" style="width: 300px">
<option>35</option>
<option>35.5</option>
<option>36</option>
<option>36.5</option>
<option>37</option>
<option>37.5</option>
<option>38</option>
<option>38.5</option>
<option>39</option>
<option>39.5</option>
<option>40</option>
<option>40.5</option>
<option>41</option>
<option>41.5</option>
<option>42</option>
<option>42.5</option>
<option>43</option>
<option>43.5</option>
<option>44</option>
<option>44.5</option>
<option>45</option>
<option>45.5</option>
<option>46</option>
<option>46.5</option>
<option>47</option>
<option>47.5</option>
<option>48</option>
<option>48.5</option>
</select>
</div>
<div class="row" style="margin-bottom: 50px">
<h2>Insert the price:</h2>
<input type="text" style="width: 300px" placeholder="$" name="price" required>
</div>
<input type="submit" class="btn btn-success btn-lg px-3" value="Sell!">
</form>
这是控制器:
public function store($shoe_id){
request()->validate(['price'=> 'required|max:255']);
// $url = request('id');
dd($shoe_id);
ListedProducts::create([
'user_id'=> auth()->id(),
// 'shoe_id'=> ,
'size'=>request('size'),
'price'=>request('price')
]);
return redirect('/home');
}
和 web.php 文件:
Route::post('sell/{product_id}', [\App\Http\Controllers\ProductsController::class, 'sell'])->name('sell');
Route::get('sell/{product_id}', [\App\Http\Controllers\ProductsController::class, 'store']);
Route::post('select',[App\Http\Controllers\ProductsController::class, 'store']);
简单的方法就是使用 HTTP GET 的查询参数。
重定向到/home
添加查询参数shoe_id
return redirect()->route('home', ['shoe_id' => $shoe_id]);
然后在您的home
控制器功能,让shoe_id
查询参数与
public function index(Request $request) {
$shoe_id = $request->input('shoe_id');
...
}
编辑:请阅读此参考。
这是您可以完成同一件事的另一种方法:
return redirect("home?shoe_id={shoe_id}");
然后在 index 方法中,您可以执行与@Ezra 建议相同的操作:
public function index(Request $request) {
$shoe_id = $request->input('shoe_id');
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.