I am putting my Controller called "LoginController" in a folder "login".
class LoginController extends BaseController{
public $restful = true;
//log in function
public function Login(){
// load the login page
return View::make('login.login');
}
}
In the routes, I give this:
Route::get('/',array('uses'=>'login.LoginController#Login'));
Also tried
Route::get('/',array('uses'=>'login\LoginController#Login'));
Route::get('/',array('uses'=>'login\Login#login'));
None of the above seem to work, and give me Class does not exist error.
I am very dumbstruck with this error. Is the way I am accessing the controller in the "uses" correct? Do I need to do any additional things before I can get it to work?
Any help really appreciated!
All you should need is
Route::get('/',array('uses'=>'LoginController#Login'));
Composer need to register this change in routes so dump-autoload composer
php composer.phar dump-autoload
Also if you are using laravel 4, then declaring restful controllers with
public $restful = true;
no longer works.
this happens to me often, just to give a different answer that worked for me
php artisan dump-autoload
Enjoy!
Yeah i had the same issue, i got my answer from https://stackoverflow.com/a/31638718/2821049
Route::group(['namespace' => 'login'], function(
{
// Controllers Within The "App\Http\Controllers\login" Namespace
Route::get('/','LoginController#login');
});
In class you adds :
namespace App\Http\Controllers\folder;
use App\User;
use App\Http\Controllers\Controller;
and in routes you call:
Route::get("admin/login","folder\class#NameFunctionInClass");
Note: folder is the name folder class contains
Related
web.php
use App\Http\Controllers\Uploadcontroller;
Route::post('/upload', [Uploadcontroller::class, 'upload']);
Uploadcontroller.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class Uploadcontroller extends Controller
{
public function upload(Request $request){
dd($request->file('image'));
}
}
These are my route and controller codes. So, when I hit upload it is showing Uploadcontroller does not exist.I am new to Laravel and I don't know what is wrong here.
use the command
php artisan route:clear
and the web.php file
add in top
use App\Http\Controllers\UploadController;
Note:
Check that the letters of the file name and class name are the same case
Your codes seems just fine.
But dont forget everytime you add a Route to the 'web.php' file you need to rebuild the routing in Laravel.
Try run this in CMD:
php artisan optimize
I am new to PHP and Laravel. I am creating my first Laravel API where I am working with Post object.
In order to do so I use a PostController. I have created this controller using the php artisan command: php artisan make:controller PostController --api. This created a PostController class inside the app\Http\Controllers.
When I want to append this controller to the api routes using the Route::resource function I came across something strange. The course I followed told me to declare the controller like so: Route::resource('posts', 'PostController');. When testing the api php artisan route:list this gave the following error:Target class [PostController] does not exist.
Then I did some research and found an other way to declare the controller inside the api routes using:
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);
This worked for me but I don't have any clue why the first declaration failed. I have looked inside my PostController class to see any typo's. But I couldn't find any:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
...
}
The reason for this is just like the exception says: it could not find the PostController in your declared namespace.
The reason for that highly depends on the Laravel version you are using. Laravel 8 removed the default namespacing to App\Http\Controllers, so if you are using Laravel 8 but have followed a tutorial for an earlier Laravel version, that might be the reason.
However, using the declaration using class imports is the better way to do it anyways, so I would stick with that.
Since Laravel 8, there were some changes to the automatic controller namespace prefixing.
https://laravel.com/docs/8.x/upgrade#routing
You can continue to use the original auto-prefixed controller routing, see the 4th paragraph on the upgrade documentation. But the new way is recommended.
You should tell Route::resource method the controller's namespace. As you did in your second try.
If you wanna do it with your first attempt, you can tell it the required namespace
Route::resource('posts', 'App\Http\Controllers\PostController');
You can also set the namespace in your App\Providers\RouteServiceProvider:
public function boot()
{
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace('App\Http\Controllers\Api')
->group(base_path('routes/api.php'));
});
}
Can't debug this simple routing issue, despite going through several similar posts.
TestController is not invokable. The controller class TestController
is not invokable. Did you forget to add the __invoke method or is the
controller's method missing in your routes file?
I have played around with it as many permutations as I can find on Stack and nothing changes it. I have confirmed that simple routing, ie:
Route::get('/', function () {
return view('welcome');
});
works, but I can't get the controllers to work. I have cleared the cache and uncommented the $namespace, nothing makes a difference. In fact, the error message doesn't seem to change, which leads me to believe it's not the routing but something to do with the Controllers. But I am a newb and am not seeing it.
from web.php:
Route::get('/test', TestController::class);
TestController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TestController extends Controller {
public function __invoke() {
return view('welcome');
}
}
This normally happens when you do not have the use statement for the FQCN of the controller.
In the routes file where you are declaring the route ensure that the use statement for the Controller namespace is added. Especially since Laravel 8.x the default namespace for controllers is not set to App\Http\Controllers unlike the earlier versions.
//routes file
use App\Http\Controllers\TestController;
Route::get('/test', TestController::class);
Just as a side note if you are using an invokable controller class to return just a view, you can use the Route::view() method instead
//Assuming that you have a resources/views/test.blade.php
Route::view('/test', 'test');
Maybe this helps someone similar to me, who overlooks a simple problem:
I wrote:
Route::post('controller/{resource}/action', \App\Http\Controllers\MyController::class, 'action')->name('controller.action');
Instead what I needed was:
Route::post('controller/{resource}/action', [\App\Http\Controllers\MyController::class, 'action'])->name('controller.action');
So basically I was missing the [] around the controller class and action params, they go together in an array - got to remember that. :)
Sorry if it is a just a tiny bit off-topic, but I searched for my problem and ended up here, therefore this just might help someone else too.
I don't know if you still need to hear this but change your
Route::get('/test', TestController::class);
into
Route::get('/test', [TestController::class, #MethodName]);
Also, call your model into the Controller.file
use App\Models\Test;
I want to use access keys for responsivefilemanager.
Since access keys can be seen by the users(example.com/filemanager/dialog.php?akey=usersaccesskeys), I want the access keys to be temporary(single use only). But I think I need "auth()" and other functions like "User::find($user_id)" to do this.
Anyways there is an error when I use auth() or controller functions in config file of the filemanager, "Call to undefined function auth() in /path/to/config/config.php".
I am stuck and I don't want to deploy my website like this.
Is there any other ways to make responsivefilemanager secure?
I have also tried using controller php file in app\Http\Controllers\RfmController.php
Other error shows "Uncaught Error: Class 'App\Http\Controllers\Controller'"
This is the function RfmController extends to.
I am running LAMP server.
PHP 7.3.7
Laravel 5.8
In my config.php
'access_keys' => array(auth()->user()->name),
by using controller.php
In my config.php
namespace App\Http\Controllers;
require('/path/to/app/Http/Controllers/RfmController.php');
$rfm = new RfmController;
.
.
.
'access_keys' => array(RFMClass::rfmakey()),
In RfmController.php
namespace App\Http\Controllers;
class RfmController extends Controller
{
public function rfmakey()
{
return auth()->user()->id;
}
{
I expected that rfmakey() would return the username
You need to use middleware. From the official laravel documentation:
If you are using controllers, you may call the middleware method from the controller's constructor instead of attaching it in the route definition directly:
public function __construct()
{
$this->middleware('auth');
}
As an aside, I would stay away from putting dynamic values or code in my config.php file, like
auth()->user()->name
This is not a good practice.
Hope this solution helps you!
The Laravel documentation clearly describes how to change your routes if you nest your controllers in folders. It seems REALLY simple, and yet I'm still getting an error. Here's the error:
"Class App\Http\Controllers\Input\InputController does not exist"
^That path looks 100% correct to me. What gives?
File Structure:
-Controllers
--Auth
--Input
---InputController.php
Routes:
Route::get('input', 'Input\InputController#getInput');
InputController:
<?php namespace App\Http\Controllers;
use Illuminate\Http\Response;
class InputController extends Controller
{
public function getInput()
{
return response()->view('1_input.input_form');
}
}
Thanks for any help!
Change Controller namespace from
namespace App\Http\Controllers
to
namespace App\Http\Controllers\Input
namespace needs to be changed to the directory your controller is in 'App\Http\Input'
You need to pull in Controller with use App\Http\Controllers\Contoller so that you can extend it.
<?php
namespace App\Http\Controllers\Input;
use App\Http\Controllers\Controller; // need Controller to extend
use Illuminate\Http\Response;
class InputController extends Controller
{
public function getInput()
{
return response()->view('1_input.input_form');
}
}
you should try running a couple commands in your base dir from your terminal (shell/prompt):
composer dump-autoload
or if you don't have composer set as executable:
php composer dump-autoload
and then:
php artisan clear-compiled
This way your laravel would prepare everything again "from scratch" and should be able to find the missing controller class.
Basically laravel generates some additional files to boot up faster. If you define a new class it doesn't get included into that "compiled" file. This way your class should be "introduced" to the framework.