2013-01-24 108 views
3

我是laravel的新手,現在正在學習它。我給在routes.php文件laravel 4中的路由問題

Route::resource('contacts', 'ContactsController'); 

以下的路徑,但是,當我打開我的網頁瀏覽器,它給了我下面的錯誤

Unhandled Exception 

Message: 

Call to undefined method Laravel\Routing\Route::resource() 
Location: 

/Users/zafarsaleem/Sites/learning-laravel/application/routes.php on line 35 

我的完整routes.php文件文件低於

Route::resource('contacts', 'ContactsController'); 

Route::get('/', function() //<------- This is line 35 
{ 
    return View::make('home.index'); 
}); 

如何消除此錯誤?

編輯

ContactsController代碼是下面,我想使用index()函數

class ContactsController extends BaseController { 

/** 
* Display a listing of the resource. 
* 
* @return Response 
*/ 
public function index() 
{ 
    Contact::all(); 
} 

/** 
* Show the form for creating a new resource. 
* 
* @return Response 
*/ 
public function create() 
{ 
    // 
} 

/** 
* Store a newly created resource in storage. 
* 
* @return Response 
*/ 
public function store() 
{ 
    $input = Input::json(); 

    Contact::create(array(
     'first_name' => $input->first_name 
     'last_name' => $input->last_name 
     'email_address' => $input->email_address 
     'description' => $input->description 
    )); 
} 

/** 
* Display the specified resource. 
* 
* @return Response 
*/ 
public function show($id) 
{ 
    return Contact::find($id); 
} 

/** 
* Show the form for editing the specified resource. 
* 
* @return Response 
*/ 
public function edit($id) 
{ 
    // 
} 

/** 
* Update the specified resource in storage. 
* 
* @return Response 
*/ 
public function update($id) 
{ 
    $contact = Contact::find($id); 
    $input = Input::json(); 

    $contact->first_name = $input->first_name; 
    $contact->last_name = $input->last_name; 
    $contact->email_address = $input->email_ddress; 
    $contact->description = $input->description; 

    $contact->save(); 
} 

/** 
* Remove the specified resource from storage. 
* 
* @return Response 
*/ 
public function destroy($id) 
{ 
    return Contact::find($id)->delete(); 
} 

} 

編輯2

我嘗試都下列路線,但最終下同錯誤

Route::resource('contacts', 'ContactsController', ['only', => ['index']]); 
Route::get('contacts','[email protected]'); 

重新安裝laravel 4現在經過我收到以下錯誤

404 Not Found 

The requested URL /contacts was not found on this server. 
_____________________________________________________________________ 
Apache/2.2.22 (Unix) DAV/2 PHP/5.3.15 with Suhosin-Patch Server at bb.dev Port 80 

編輯3

這裏是什麼現在所做的,我編輯 「/private/etc/apache2/users/.conf」 和從「AllowOverride None」更改爲「AllowOverride All」,然後重新啓動我的apache服務器。現在我得到以下錯誤

403 Forbidden 

You don't have permission to access /contacts on this server. 
__________________________________________________________________________________ 
Apache/2.2.22 (Unix) DAV/2 PHP/5.3.15 with Suhosin-Patch Server at bb.dev Port 80 

爲什麼我沒有該聯繫人控制器的權限?現在讓我發瘋。

這裏是我的.htaccess文件

<IfModule mod_rewrite.c> 
Options -MultiViews 
RewriteEngine On 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^index.php [L] 
</IfModule> 
+0

路由可以是GET,POST,把....只是將其更改爲適當的請求。另請閱讀路由文檔。 – itachi

+0

@itachi這個[link](http://laravel.com/docs/routing)上的路線文件沒有說路線必須是獲得,發佈,放等等。如果你閱讀了關於這個[link](https: //github.com/laravel/framework/issues/34)你會發現我正在使用的也是可能的。 – 2619

+0

默認情況下,據我所知,laravel支持get,post,put,head和delete請求。如果您打開框架並查看路由是如何完成的,那麼您會發現與每個請求都有關聯的特定方法。如果您使用捆綁包,則會出現問題。你在用嗎?如果是,哪一個? – itachi

回答

0

這可能是一個命名空間的問題 - 應該在調用函數照亮 \路由\路由器,但你的異常是指Laravel \路徑\路線::資源()。

是否有可能您的配置文件仍然在其中引用了Laravel3?

+0

不,我從來沒有使用過laravel。這是我第一次使用它,並開始使用laravel 4.我過去沒有安裝laravel 3 – 2619

+0

我重新安裝了整個laravel再次在youtube上播放其中一個截屏視頻,但是這一次它說'Not Found 在這臺服務器上沒有找到請求的URL /聯繫人' – 2619

9

你在另一臺服務器上試過這個嗎?很多事情可能會因爲重寫而出錯(我已經在修復.htaccess中失去了幾個小時),所以問題可能是Apache而不是Laravel。

這個工作對我來說在public/.htaccess

<IfModule mod_rewrite.c> 
    RewriteEngine on 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 

    RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule> 

而且你嘗試去index.php/contacts,而不是/contacts?如果可行,問題是Apache,而不是Laravel。

+0

您已經保存了我的一天! –

0

試着改變你的路線

Route::resource('/contacts', 'ContactsController'); 

在ContactsController.php變化指數迴歸模型

public function index() 
{ 
    return Contact::all(); 
} 
0

我有同樣的$#%&問題的情況下,和搜索小時後我發現它不是.httacess文件的問題。我只是做了解決這個問題:

composer update --dev 

我認爲--dev位是最重要的事情。希望能幫助別人。

0

好吧,我有問題達1分鐘前! 這是因爲IDE輔助 來解決,你應該註釋下面的代碼在routes.php文件

use Illuminate\Routing\Route;