Laravel url routes - php

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'));
}
}

Related

Why Suddenly Laravel started showing 'page not found' error?

I was working on my project and suddenly Laravel started showing Sorry, the page you are looking for could not be found. error. Here is how my web.php, CarController.php and view's location looks like.
web.php
Route::group(['middleware'=>'auth'],function(){
Route::get('cars/create',"CarController#create");
}
CarController.php
<?php
namespace App\Http\Controllers;
use App\Car;
use Illuminate\Http\Request;
use Auth;
class CarController extends Controller
{
public function create()
{
return view('cars.create');
}
}
View cars.create exist at /resources/views/cars/create.blade.php.
I don't know what caused this error but It's really irritating. I think this error came into existence because I had created one model named Request which might conflict with the actual Request class made by Laravel.
But, As soon as I saw the error, I deleted that Model including migration and controller. But still, the error is there. Any help would be appreciated!
Route::get('cars',"CarController#index");
Route::get('cars/{car}',"CarController#show");///remove this route definition or keep it at the end of your route file
Are you logged in ?? when you're trying to get the route??
usually what happens is the session expires after sometime.

Do I need to have Controller.php in all my API subversion?

I am trying to create an API directory in my Laravel project and I'm receiving the following error...
Class 'App\Http\Controllers\Controller' not found
I have tried using use App\Http\Controllers\Controller; on top of my API controllers but no luck.
My API classes are defined like...
class SyncController extends Controller {...}
class TimecardController extends Controller {...}
I'm pretty sure the error is coming from the extends Controller portion.
in App\Http\Controllers\Controller I have Controller.php. One way I have pushed past this is to duplicate the Controller.php into App\Http\Controllers\Api\v2\ and change the namespace of that controller to match where it is located (namespace App\Http\Controllers\Api\v2;)
I don't believe this is correct, as there should be a way to reference the Controller.php from the controllers in the API subdirectory.
../Controllers/Controller.php and API is a subdirectory, ../Controllers/Api/v2/SyncController.php
Any help would be much appreciated.
Thanks
-----------Edit------------
my routes for the api look like so
Route::group(['prefix' => 'api/v2'], function () {
Route::get('sync', 'Api\v2\SyncController#sync')->middleware('auth:api');
Route::post('timecard', 'Api\v2\TimecardController#create')->middleware('auth:api');
});
The Controller class cannot be found because the API controllers are not in the default Laravel controller directory. You need to add the controller class as a use statement. Then the autoloader will be able to find it.
namespace App\Http\Controllers\Api\v2;
use App\Http\Controllers\Controller;
class SyncController extends Controller {...}
And while your at it you might also want to add the auth:api middleware to the entire group. Much safer and efficient.
Route::group(['prefix' => 'api/v2', 'middleware' => 'auth:api', 'namespace' => 'Api\v2'], function () {
Route::get('sync', 'SyncController#sync');
Route::post('timecard', 'TimecardController#create');
});

Laravel 5.5: Controller does not exist

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.

Class App\Http\Controllers\homeController does not exist

i'm using laravel 5 , in rutes.php i have this code :
Route::get('about',"homeController#about");
and in App\Http\Controllers\ i have file homeController.php that contains :
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller as BaseController;
class homeController extends BaseController{
public function about(){
return view::make('about');
}
}
but it throws this error : Class App\Http\Controllers\homeController does not exist .
how can i fix it ?
here is structure of the project and controllers :
First, check if you write the name of the controller correctly.
If it is, there are 3 methods:
Route::get('/about', 'App\Http\Controllers\homeController#about');
Write all the paths where your controller there is.
Route::get('/about', [HomeController::class, 'about');
Go to file RouteServiceProvider.php in Providers folder
Search for //protected $namespace = 'App\\Http\\Controllers';
( You will find it in comment)
Remove // of comment and save.
with this method, you will able to use the name of the controller directly without
writing all the paths.
Change all
homeController
To
HomeController
For HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function welcome()
{
return view('welcome');
}
}
For web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'welcome']);
Its work for me
change your routing like below:
use App\Http\Controllers\HomeController;
Route::get('/about', [HomeController::class, 'index'])->name('home');
for more info look at this page:
https://laravel.com/docs/8.x/routing
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller as BaseController;
class homeController extends BaseController{
public function about(){
return view::make('about');
}
}
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller as BaseController;
class homeController extends BaseController{
public function about(){
return view::make('about');
}
}
Should works perfectly. Are you sure that name of file is homeController.php ?
I faced a similar issue with the Home controller, in my case I saw the route was configured as following:
Route::get('/home',"homeController#index");
when I changed the above code to the following then it worked.
Route::get('/home',"App\Http\Controllers\HomeController#about");
In your case first, check the spelling first, whether it should be HomeController or homeController.
You can change your route code to the following
so you can try to change the following code
Route::get('about',"HomeController#about");
to
Route::get('about', 'App\Http\Controllers\HomeController#about');
or
Route::get('/about', 'App\Http\Controllers\HomeController#about');
Hope this will work.
It can happen because of these two causes:
Typo Error
Laravel Cache
1) Type Error
For this check, please check your HomeController file name and the class name in that file. Both should be same with case sensitivity.
HomeController.php
class HomeController extends
2) Laravel Cache
Laravel stores file caches with previous configurations. To refresh the cache, do this commands in the command window and then try again
php artisan cache:clear
php artisan view:clear
php artisan optimize
Hope it solved!.
Yes. That is right because you are not giving right path to the action in the routes. Either you update core files for path or provide manually in the route. e.g
you have
Route::get('about',"homeController#about");
try this route
Route::get('/about', [App\Http\Controllers\homeController::class,'about']);
or you can type route as
Route::get('/about', 'App\Http\Controllers\homeController#about');
furthermore you can check if you have right code in the controller function.
You are trying Laravel 7.x and before routing schema. Refer to 8.x documentation.
New syntaxis using as [HomeController::class, 'index'] or you need to add namespace before Controller name like App\Http\Controllers\HomeController.
Update the path to your route by adding .
Route::get('/home', [\App\Http\Controllers\HomeController::class, 'index'])->name('home');
Please update your route
use App\Http\Controllers\HomeController;
Route::get('/about', [HomeController::class, 'about']);

url in routes.php not rendering in laravel 4

I am new to laravel. I am just trying to do some basic stuff using routes and controllers.
I have this controller named testcontroller which looks as below
<?php
class testcontroller extends BaseController{
public function show(){
return 'test';
}
}
I have also defined a route in routes.php as below
Route::get('test', 'testcontroller#show');
when i try to access it using localhost/laravel/public/test it would give the 404 NOT FOUND error, however i have tried rewriting the route in main directory as
Route::get('/', 'testcontroller#show');
and it works all fine, i have even tried and successfully rendered views using this. Can anyone suggest where am i going wrong with the url. Any help will be appreciated.
Thanks :)
Test controller
# app/controllers/TestController.php
class TestController extends BaseController {
public function show()
{
return 'test';
}
}
Routes File
# app/routes.php
Route::get('test', array(
'uses' => 'TestController#show'
));
Route::get('/', array(
'uses' => 'TestController#show'
));
Edit: since that is not working, it is probably to do with your server configuration. Instead of learning how to do that, I'd recommend the use of a VM over WAMP. Also Laravel provides an easy way to use one with Laravel Homestead.
Check out this instructional video on how to do that.

Categories