Problem with routes in laravel 4
I am new to laravel and am learning it now. I give the following route in a fileroutes.php
Route::resource('contacts', 'ContactsController');
But when I load my page in the browser it gives me the following error
Unhandled Exception
Message:
Call to undefined method Laravel\Routing\Route::resource()
Location:
/Users/zafarsaleem/Sites/learning-laravel/application/routes.php on line 35
My complete route.php file is below
Route::resource('contacts', 'ContactsController');
Route::get('/', function() //<------- This is line 35
{
return View::make('home.index');
});
How do I remove this error?
Edit
ContactsController code is below and I want to use index () function
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();
}
}
Edit 2
I tried both of the following routes but ended up below error
Route::resource('contacts', 'ContactsController', ['only', => ['index']]);
Route::get('contacts','ContactsController@index');
After reinstalling laravel 4, now I am getting the following error
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
Edit 3
Here is what I now edited "/private/etc/apache2/users/.conf" and changed from "AllowOverride None" to "AllowOverride All" and then restarted my Apache server. Now I am getting the following error
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
Why don't I have permission for this contact controller? Now it makes me crazy.
Here is my .htaccess file
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
source to share
Have you tried this on another server? A lot of things can go wrong with the rewrite (I lost hours fixing .htaccess), so the problem might be Apache, not Laravel.
This works for me in public/.htaccess
:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
And have you tried moving to index.php/contacts
instead /contacts
? If this works, the problem is with Apache, not Laravel.
source to share