Denis Saidov Denis Saidov - 3 months ago 24
PHP Question

new controller mistake Laravel



I was testing out while learning Laravel 5, creating a new controller, but I do get a strange error, when it is supposed to work fine. Here it is:

I created a new folder Pages in the app/Http/Controllers/ directory, and added the controller ContactController into it:

app/Http/Controllers/ContactController.php


<?php

namespace App\Http\Controllers\Pages;

use App\Http\Controllers\Controller;

class ContactController extends Controller {

public function getIndex() {
return 'This is the Contact page.';
}
}


and then I created a route in the routes/web file:

routes/web.php


Route::controller('contact', 'Pages\ContactController');




Strangely, when I access the /contact URL -> http://laravel.devpeaks.com/public/contact, I get:

BadMethodCallException in compiled.php line 6271:
Method controller does not exist.

Answer
  1. You should adjust your nginx/apache config in a way that root directory is pointed to /public.
  2. Route::controller is deprecated since Laravel-5.2. You should use Route::resource and name rename the method from getIndex to index. If you use Laravel-5.1 implement the first point only.
Comments