Laravel 5.5: Controller does not exist - php

Coming from CodeIgniter, I decided that it's time for something new and went for Laravel. I love the syntax of the framework and how clean it is, however, I am overwhelmed by how complicated simple things seem to be. I have a controller and want to link to a function in said controller. Whatever I do, I keep getting this error:
ReflectionException in Route.php line 280:
Class App\Http\Controllers\TasksController does not exist
I have Googled this issue but I just can't seem to figure it out. It seems like I got everything right but then again, I'm completely new to this framework so I don't really know. I have a namespace, a route and all of that stuff. Anyway, here is my code:
The link
<a class="nav-link" href="<?= url('tasks') ?>">Tasks</a>
My route in routes.php
Route::resource('tasks', 'TasksController');
TasksController.php
<?php
namespace App\Http\Controllers\Controller;
use App\User;
use App\Http\Controllers\Controller;
class TasksController extends Controller
{
public function index()
{
$tasks = DB::table('tasks')->get();
return view('tasks', ['tasks' => $tasks]);
}
}
Thank you for any answers and if you need more information, please say so.

Given that you are using default Laravel installation, the current directory of controllers does not exist.
Try changing
<?php
namespace App\Http\Controllers\Controller;
to
<?php
namespace App\Http\Controllers;
in your TasksController.php file

Your namespace (in controller file) is App\Http\Controllers\Controller and should be App\Http\Controllers.

Related

ReflectionException Class App\Http\Controllers\PostsController does not exist Lumen 5.5

I am new to Lumen, and I got error when using Postman: ReflectionException Class App\Http\Controllers\PostsController does not exist
Here is my PostsController.php
namespace App\Http\Controller;
use App\Post;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PostsController extends Controller{
/*content*/
}
I already checked location of my controller. I also read the same problem and tried the solutions, but it was not work for me. Can anyone tell me how to fix this? Thanks before
Controller's namespace is wrong, Check the namespace. Good luck
namespace App\Http\Controllers;
You have wrong namespace in your controller file.
You need to change it to Controllers
namespace App\Http\Controllers;

use a controller inside a folder in the controllers folder

I'm trying to use a controller inside a folder in the controllers directory like
route
Route::get('/','site\HomeController#index');
but seem's does not work like it gives me this error
Class App\Http\Controllers\site\HomeController does not exist
Note: I have also a HomeController.php in the controllers folder. I'm trying to organize my controllers by putting them unto their specific folders.
Any help, ideas please?
You should use proper namespace, like:
namespace App\Http\Controllers\Site;
And add this line:
use App\Http\Controllers\Controller;
Then this route will work:
Route::get('/','Site\HomeController#index');
The namespace of the class HomeController should be as:
namespace App\Http\Controllers\Site;
And in your route file you can use it as:
Route::get('/','Site\HomeController#index');
Remember to add following line of code in HomeController class as:
use App\Http\Controllers\Controller;

How do I access this in Laravel 5.2?

I'm using Laravel 5.2 and I've followed the instructions stated here.
However, when I attempt to access it in my controller, I get an error:
Class 'App\Http\Controllers\IPBWI' not found # line 12
<?php
namespace App\Http\Controllers;
use Haslv\Ipbwi;
MyController extends Controller {
public function index() {
$member_info = IPBWI::member()->info(); //line 12
//etc
}
}
I understand what's wrong but I don't understand how do to correctly reference it.
Could you help me out?
I'm not sure where you got this, but I would take it out:
use Haslv\Ipbwi;
If you want to use Laravel's facade and you followed the instructions on the github page, then you should add this to the top of your controller:
use IPBWI;
This is also case-sensitive so make sure that it matches the case in this line of code in your config/app.php file:
'IPBWI' => 'Haslv\Ipbwi\Facade',
You need to move your namespace and use statement above the class declaration.
<?php
namespace App\Http\Controllers;
use Haslv\Ipbwi;
class MyController extends Controller {
// controller code
}

How can I correctly setup models in Laravel 5 and fix my error?

I am currently learning Laravel 5. I have connected my database. Setup routes, created a controller, created a view and attempted a model, which is where I need help.
I used php artisan to create my model, which is in the /app directory.
When I try to visit /myer on the browser. I am getting the following error:
FatalErrorException in MyersController.php line 20:
Class 'App\Http\Controllers\Myer' not found
I have put the edited files on http://www.filedropper.com/help
I have no idea where I have gone wrong, I have messed around using "use" and ultimately all I get is that the Class can't be found. This is beginning to destroy my soul. If someone can help me, I would be forever grateful!!
Files
From MyersController.php
public function index()
{
$myers = Myer::all();
return view('myers.index')->with('myers'.$myers);
}
From routes.php
Route::get('/myer/', 'MyersController#index');
Route::resource('myer','MyersController');
From Myer.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Myer extends Model
{
//
}
From index.blade.php
<h2>Myers</h2>
<ul>
#foreach ($myers as $list)
<li>{{{ $list->name }}}</li>
#endforeach
</ul>
As you can see in the error, it tries to find the model in the same namespace as your controller: FatalErrorException in MyersController.php line 20: Class 'App\Http\Controllers\Myer' not found. In the Model, you can see it's in namespace App.
So either put
use App\Myer;
in the top of your controller under the namespace, or reference the full path where you need it:
public function index()
{
$myers = App\Myer::all();
return view('myers.index')->with('myers'.$myers);
}
But if you will use it more often in this controller, then it's more efficient to put it in the use.
P.S. Please don't let it destroy your soul.

Laravel url routes

I have encountered a problem with my routes in Laravel.
Im fairly new to laravel and coding in general, but i had no problems with my routes when working on Mint.
Now i switched to win8 and i can't seem to acces localhost/about.
I get this error:
Class App\Http\Controllers\PagesController does not exist
But my routes.php look like this:
Route::get('about', 'PagesController#about');
Under http>Controllers i have created PagesController.php
yet i get the error that PagesController does not exist.
Can someone please help me, feel like i've tried 100 options, from previous threads.
I have rewrite enabled, i've modified httpd.conf and my .htaccess
Here is my controller,
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controllers;
use Illuminate\Http\Request;
class PageController extends Controller {
public function about() {
$first = "Nichlas";
return view ('pages.about', compact('first'));
}
}

Categories