2016-09-23 35 views
-1

LARAVEL 5.0 PHP 5.4.45Laravel形式包括在輸入動作屬性

我有一個形狀像這樣的路徑:

/應用/ HTTP /路由。 php

Route::get('/clients/search/{id}', '[email protected]')->where('id', '[0-9]+'); 
Route::get('/clients/search/{text}', '[email protected]')->where('text', '[a-zA-Z]'); 

我不會在這裏打印我的視圖,但它只是搜索確切的客戶端(案例ID)或前10個客戶端(案例文本)。

然後我想創建一個搜索表單。我創建的路線:

/app/Http/routes.php

// Route::get('/clients/search/{id}', '[email protected]')->where('id', '[0-9]+'); 
// Route::get('/clients/search/{text}', '[email protected]')->where('text', '[a-zA-Z]'); 
Route::get('/clients/search', '[email protected]'); 

該路由控制器:

/app/Http/Controllers/ClientController.php

<?php  

namespace App\Http\Controllers; 

use App\Http\Requests; 
use App\Http\Controllers\Controller; 
use DB; 

class ClientController extends Controller { 
    // Controllers for the 'searchById' and 'searchByText' 

    public function search() { 
     return view('client.search.search', [ 
      'title'  => 'Client search form', 
      'title_sub' => '' 
     ]); 
    } 
} 

而對於這個搜索表單視圖:

/ressources/view/client/search/search.blade.php

<form action="/clients/search/INPUT HERE ?" method="get"> 
    <div class="input-group"> 
     <input id="newpassword" class="form-control" type="text" name="password" placeholder="Id de client, nom, prénom, ...">    
     <span class="input-group-btn"> 
      <button id="button_search" class="btn green-haze" type="submit"><i class="fa fa-search fa-fw"></i></button> 
     </span> 
    </div> 
</form> 

問題

哪有我,在提交之前,將我的表單的屬性action的一部分作爲輸入的一部分傳入?關鍵是要能夠推出這些類型的請求:

  • /客戶/搜索/ 26
  • /客戶/搜索/麥克%20%Folley
  • /客戶/搜索/巴黎

所以我的控制器處理這條路線可以完成這項工作。有沒有辦法做到這一點?或者我應該去尋找JavaScript解決方案(,這讓我感到難過)?

+0

如果你想改變表單的動作屬性,你必須使用JavaScript。但爲什麼你需要那個?爲了幫助您的路線決定兩個搜索控制器之間?在代碼中有一個搜索控制器會決定 - 根據搜索術語的類型 - 是按ID還是按名稱搜索? – Matey

+0

這取決於它是一個文本還是一個id,我的視圖是不一樣的:如果它是一個id,每個客戶端在數據庫中都有一個唯一的Id,我獲取確切的客戶端信息,所以我的視圖不同。如果是文本搜索,它可能有幾個客戶端匹配這個文本(例如:搜索「mike」,數據庫中可能會有多個「Mike」,因此我將它們存儲在數組中而不是特定的客戶端視圖) 。我設法使用JavaScript進行搜索工作。也許如果我以另一種方式來解決問題,我可以使用窗體參數進行管理。太可怕了,我猜是可能的。 –

+0

一個控制器可以返回不同的視圖。 – Matey

回答

1

是的,你需要javascript來修改請求的URL,然後才發送:)。沒有必要對這種形式的標籤,像這樣的香草的js方法可能就足夠了:

<input type="text" id="search"> 

<button onclick="search()"> 
Submit 
</button> 

<script> 
function search() { 
    window.location='/search/' + 
    encodeURIComponent(document.getElementById('search').value); 
} 
</script> 

你可以和應該只使用一個路徑,然後在控制器的方法決定如何搜索和返回什麼看法。路線應該包含儘可能少的邏輯。