2014-06-19 80 views
0

我要實現以下目標:Laravel 4列表和詳細路線

設備>控制器@設備

設備/(編號)>控制器@設備

這可能與Laravel?我試圖用android應用程序ImperiHome映射一個domotic盒子,並且他們希望我有相同的路由設備列表和任何設備操作。

到目前爲止,我已經試過這樣:

Route::get('devices/{deviceId}/action/{actionName}/{actionParam?}', '[email protected]'); 
Route::get('devices', '[email protected]'); 

但我不能檢索參數時,我稱之爲設備/ ID網址

+0

這應該工作,你如何試圖檢索參數?嘗試翻轉兩條路線的順序,當我在過去做過類似的事情時,接受參數的路線總是在沒有路線的路線之後。 – Jeemusu

+0

好吧,我找到了一種方法使其工作,問題是我無法將兩個路由重定向到相同的方法,所以我只使用了2種方法(設備和設備) – kitensei

+0

您可以將多個路由重定向到單個方法。如果你擴展你的問題,有人可能會幫助你。解釋當你嘗試訪問帶有id的路由時會發生什麼,這將是一個好的開始,所以發佈你的'devices'方法。 – Jeemusu

回答

0

好了,解決了PHP的嚴格標準的錯誤,我只是分裂路由到兩個方法如下:

routes.php文件

Route::get('devices/{deviceId}/action/{actionName}/{actionParam?}', '[email protected]'); 
Route::get('devices', '[email protected]'); 
Route::get('rooms', '[email protected]'); 
//Route::get('action_ret', '[email protected]_ret'); 
Route::get('system', '[email protected]'); 
Route::get('/', '[email protected]'); 

DomoticzController.php

/** 
* Call for an action on the device identified by $deviceId. 
* @return string Json formated action status. 
*/ 
public function device($deviceId, $actionName, $actionParam = null) 
{ 
    $client = $this->getClient(); 
    $request = $client->getClient()->createRequest('GET', get_url("json.htm?type=command&param={$actionName}&idx={$deviceId}}&switchcmd=$actionParam")); 
    $response = $request->send(); 
    $input = $response->json(); 

    // convert to app format 
    $output = array('success' => ('OK' === $input['status'] ? true : false), 'errormsg' => ('ERR' === $input['status'] ? 'An error occured' : '')); 

    return Response::json($output); 
} 

/** 
* Retrieve the list of the available devices. 
* @return string Json formatted devices list. 
*/ 
public function devices() 
{ 
    $client = $this->getClient(); 
    $request = $client->getClient()->createRequest('GET', get_url('json.htm?type=devices&used=true')); 
    $response = $request->send(); 
    $input = $response->json(); 

    // convert to app format 
    $output = new stdClass(); 
    $output->devices = array(); 

    foreach ($input['result'] as $device) { 
     $output->devices[] = array (
      'id' => $device['idx'], 
      'name' => $device['Name'], 
      'type' => 'DevSwitch', 
      'room' => null, 
      'params' => array(), 
      ); 
    } 

    return Response::json($output); 
} 

也許有解決這個更好的方法,我會很高興聽到這個消息。

0

如果你讓兩個路由使用相同的控制器動作,你需要在控制器中設置可選的參數。

試試這個public function device($deviceId = null, $actionName = null, $actionParam = null)看看你是否仍然得到PHP嚴格錯誤。

不能將沒有參數的路由重定向到需要參數的控制器操作。另一方面,您可以將帶有參數的路由重定向到帶有可選參數的控制器操作(這並不意味着您的路由參數必須是可選的)。

+0

是的,這就是我想,但我寧願有2種方法,因爲第二種方法必須有前2個參數,並且用你的例子,我可以輕鬆地調用它,而無需任何參數 – kitensei