Om3ga
Om3ga

Reputation: 32941

Routes issue in laravel 4

I am new to laravel and learning it now. I am giving following Route in routes.php file

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

But when I load my page in browser, it gives me 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 routes.php file is below

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

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

How can I remove this error?

Edit

ContactsController code is below and I want index() function to be used

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 following routes but ended up same below error

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

After reinstalling laravel 4 now I am getting 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 did now, I edit "/private/etc/apache2/users/.conf" and changed from "AllowOverride None" to "AllowOverride All" and then restarted my apache server. Now I am getting 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 contacts controller? It is making me crazy now.

Here is my .htaccess file

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

Upvotes: 3

Views: 7650

Answers (5)

alex
alex

Reputation: 7905

ok i had the issue up to one minute ago! it is because of ide-helper to solve you should comment the following code in routes.php

use Illuminate\Routing\Route;

Upvotes: 0

Luisinho Rojas
Luisinho Rojas

Reputation: 199

I had the same $#%& issue, and after hours of searching I found it is not a problem of the .httacess file. What I just did to fix this:

composer update --dev

I think that the --dev bit is the important thing. Hope that helps someone.

Upvotes: 0

mogetutu
mogetutu

Reputation: 508

Try changing your routes to

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

In ContactsController.php change index to return instance of the model

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

Upvotes: 0

spacek33z
spacek33z

Reputation: 2253

Have you tried this on another server? A lot of things can go wrong with rewriting (I've lost hours fixing the .htaccess), so the problem may be Apache and 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 going to index.php/contacts instead of /contacts? If that works, the problem is Apache, not Laravel.

Upvotes: 9

J.T. Grimes
J.T. Grimes

Reputation: 4272

It may be a namespace issue -- it should be calling the function on Illuminate\Routing\Router but your exception refers to Laravel\Routing\Route::resource().

Is it possible that your configuration file still has references to Laravel3 in it?

Upvotes: 0

Related Questions