2014-03-25 66 views
0

這是我的laravel的控制器函數。laravel controller如何將變量從一個函數傳遞給其他函數

public function getDefault() 
    { 
return View::make('layouts.default'); 
} 

    public function getURL() 
{ 
    $name = Input::all(); 
    $url=$name['url']; 
    $json_data=file_get_contents($url); 
    $content=json_decode($json_data,true); 
    $this->getThemeOne($content); 
    return View::make('hello'); 

} 

怎樣的$內容的數據傳遞給所有功能:

public function getThemeOne($content) 
{ 
    return View::make('themes.home')->with('data',$content); 
} 


public function getAbout($content) 
{ 
    return View::make('themes.about')->with('content',$content); 
} 

航線代碼:

 Route::get('/','[email protected]');//home page 
    Route::get('/view','[email protected]'); 
    Route::post('/', '[email protected]'); //post url from home page 

視圖頁面:

hello.blade.php: 
{ link to goto home.blade.php}-> 
<a href="view">home</a> 

home.blade.php: 
{{$data['about']}} 

請幫助我如何這樣做...

+0

getURL()被調用的地方是什麼? –

+0

問題是否解決?如果仍然出現參數錯誤,請確保您的'json_decode()'返回值是有效的。 – Patrick

+0

從json_decode返回()是有效的...我想告訴你,我有20個控制器中的功能需要$內容數據...有無論如何通過一個函數.. – user3459007

回答

0

更新:

要共享翻過視圖中的數據,你可以在你的函數getURL()使用:

View::share('content', $content); 

您可以通過$content作爲參數傳遞給你的函數和你的看法是這樣的:

public function x { 
    $name = Input::all(); 
    $url=$name['url']; 
    $json_data=file_get_contents($url); 
    $content=json_decode($json_data,true); 

    $this->getThemeOne($content); // or whatever your scope is 
} 



public function getThemeOne($content) 
{ 
    return View::make('themes.home')->with('content', $content); 
} 

請參閱該文檔http://laravel.com/docs/responses#views

+0

感謝您的回覆,但它不工作......錯誤是DataController :: getThemeOne()缺少參數1。 – user3459007

+0

你能發佈所有相關的代碼嗎?你是否通過了一個論點?錯誤說你沒有通過參數... – Patrick

+0

m laravel中的新...請告訴我如何訪問查看頁面上的共享變量 – user3459007

相關問題