Laravel controllers not working - php

I'm learning laravel but it isn't working out that well....
I've set my route in routes.php:
Route::get('/','WelcomeController#index');
Then I obviously have made a controller called "WelcomeController" and it looks like this:
<?php
class WelcomeController extends BaseController
{
public function index()
{
return view ('index');
}
}
?>
And then I've made a view called index with just some html text.
But when I go to localhost/public I receive the error:
FatalErrorException in WelcomeController.php line 3:
Class 'BaseController' not found
And when I say:
class WelcomeController extends Illuminate\Routing\Controller
It does not work!
What am I doing wrong.

You should try
use Illuminate\Routing\Controller as BaseController;
at the top of your controller file. That acts as an import

Two suggestions:
Run php composer dump-autoload to make sure the class mappings is fresh.
Add use Controller; in your use block. The modify your controller to extend it. Example:
class WelcomeController extends Controller {...
Controller is an interface in Laravel 4.*
In Laravel 5 use instead:
use App\Http\Controllers\Controller; according to the documentation here: http://laravel.com/docs/5.0/controllers

Related

Fatal Error Class 'App\Http\Controllers\Controller' not found Laravel 5.4

I tried to route with using name .
I already tried to solve with using Composer update , Clear Cache , Generate App key.
But it's showing fatal error .
Error View Message :
(1/1) FatalErrorException
Class 'App\Http\Controllers\Controller' not found
in ClassesController.php line 13
My Route Code Below :
<?php
Route::prefix('Classes')->group(function(){
Route::get('/add','ClassesController#index')->name('AddNewClass');
});
My Controller Code Below :
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ClassesController extends Controller{
public function index()
{
return "Method Access";
}
In this event, I would first ensure that the file containing the class Controller exists in the same directory with the same namespace as the ClassesController, as implied in your code.
If the Controller does not exist, you can either create the class or simply remove the extends Controller and the line use App\Http\Controllers\Controller; from your code.
its worked for me.
usercontroller
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Show the profile for the given user.
*/
/* http://localhost:8080/laravelapps/blog/public/staff */
public function showProfile()
{
return 'new';
}
}
web.php
Route::get('/staff', 'UserController#showProfile');
http://localhost:8080/laravelapps/blog/public/staff
Check whether Controller.php file exists or not in your Controllers folder if not exists download or put from laravel project.
After that run following command php artisan app:name newproject
Make sure in the above line i have updated text 'newproject' that should be first line of your Controller.php file. For example: namespace newproject\Http\Controllers;

Model Class not found error

Using Laravel 5.4, I am getting this error on view whenever I call my method it shows on screen
(1/1) FatalThrowableError
Class 'App\Models\Chat\User' not found
Hierarchy of my project :
Controllers
- ChatMessageController
Models
-Chat
-message.php
-User.php
and here's my controller class code :
namespace App\Http\Controllers\Chat;
use App\Http\Controllers\Controller;
use App\Models\Chat\Message;
use App\Models\User;
use Illuminate\Http\Request;
class ChatMessageController extends Controller
{
public function index()
{
$messages = message::with(['user'])->latest()->limit(100)->get();
return response()->json($messages,200);
}
}
In message.php, probably you forgot to add: use App\Models\User;.
So, it is trying to find User in the wrong space.

Class Controller not found in laravel 5

So I am new to laravel and was just trying out some code to clear the basics,but however after creating a new controller to handle a route.It throws a fatal exception saying Class 'Controller' not found!
(The controller.php exists in the same directory)
The controller.php code is the default one
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
abstract class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
This is my PagesController.php code
<?php
class PagesController extends Controller
{
public function home()
{
$name = 'Yeah!';
return View::make('welcome')->with('name',$name);
}
}
This is route.php code
<?php
Route::get('/','PagesController#home');
The welcome.blade.php code is the default one except it displays the variable $name instead of laravel 5.
What am I getting wrong?
When you reference a class like extends Controller PHP searches for the class in your current namespace.
In this case that's a global namespace. However the Controller class obviously doesn't exists in global namespace but rather in App\Http\Controllers.
You need to specify the namespace at the top of PagesController.php:
namespace App\Http\Controllers;
You will want to specify the namespace to your Controller class:
class PagesController extends \App\Http\Controllers\Controller
otherwise Controller is looked up in the default root namespace \, where it does not exist.
My issue was a different class name than the one in the class that extends controller, the names should be same
If you are creating a controller inside a subfolder with a different namespace then use this on your controller file:
use App\Http\Controllers\Controller;

Laravel can't find class in namespace

I using Laravel, I have a Model class under App/Models
<?php
namespace App\Models;
class TodoList extends \Eloquent{
public function listItems(){
return $this->hasMany('TodoItem');
}
}
In my Controller I have included the namespace as follows:
namespace App\Http\Controllers;
...
use App\Models\TodoItem;
use App\Models\TodoList;
class TodoListController extends Controller
My method looks like this:
public function show($id)
{
$list=TodoList::findOrFail($id);
return \View::make('todos.show')->with('list', $todo_list);
}
but when I call to a request i get the error:
FatalErrorException in TodoListController.php line 75: Class
'App\Models\TodoList' not found
Trying running composer dump-autoload. Basically your classes become cached so you need to tell Laravel to look for newly added classes.
I'm not sure, just try this :
namespace App\Models;
use Illuminate\Database\Eloquent\Model as Eloquent;
class TodoList extends Eloquent{
public function listItems(){
return $this->hasMany('TodoItem');
}
}
Here is the code from the docs, but for your example. Note the use Model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TodoList extends Model {
//insert public function listItems() here
}
Hope this is helpful!
Make sure the file is in the correct folder and has the same name caption as the Class you want to import.
If the caption of the file "TodoList" is not TodoList.php but Todolist.php for example, Laravel wont find it.
Depending on your setup running "composer dump" might help to refresh autoload files.

Using Laravel RESTfull with namespacing

I have a problem. I am building an administration panel for my application and decided, because of the many functions, to use a RESTfull route. Now, because I do not want to jam every function in the same class I also use namespacing and extend my AdminController class.
The problem is, RESTFull works for the functions declared in the AdminController file, but it does not recognize the functions deeper within the namespace. What is the correct way to do this?
This is the code I have right now:
RESTfull Route
Route::controller('admin', 'Admin\AdminController');
AdminController (/controllers/AdminController.php)
namespace Admin;
use View;
class AdminController extends \BaseController {
public function getSales() {
echo"Works";
}
DashboardController (/controllers/admin/DashboardController.php
namespace Admin;
use AdminController;
use View;
class DashboardController extends AdminController {
public function getDashboard() {
echo"Does not work";
}
I can access www.domain.com/admin/sales just fine, but when I access www.domain.com/admin/dashboard it gives me a "Controller method not found" error.
I think you should provide this route manually:
Route::controller('admin/dashboard', 'Admin\DashboardController');
In your code Laravel has no idea that it should use DashboardController and not AdminController for admin/dashboard route

Categories