When I create a controller in laravel 5.4 I get this error
In RouteAction.php line 84:
Invalid route action:
[App\Http\Controllers\Admin\DashboardController].
I do not create Admin/DashboardController. Still makes a errors
web.php
Route::group(['namespace' => 'Admin', 'middleware' => ['auth:web', 'CheckAdmin'], 'prefix' => 'admin'],function (){
$this->resource('authorities', 'AuthoritiesController');
$this->resource('complaints', 'ComplaintsController');
$this->resource('schools-list', 'SchoolsListController');
$this->resource('inspection-failed', 'InspectionFailedController');
$this->resource('inspection-register', 'InspectionRegisterController');
$this->resource('inspection-results', 'InspectionResultsController');
$this->resource('inspectors-list', 'InspectionListController');
$this->resource('investigators', 'InvestigatorsController');
$this->resource('notification-infringement', 'NotificationInfringementController');
$this->resource('system-experts', 'SystemExpertsController');
$this->resource('submit-information', 'SubmitInformationController');
$this->resource('primary-committee-meeting', 'PrimaryCommitteeMeetingController');
$this->resource('list-violations-school', 'ListViolationsSchoolController');
$this->resource('announcing', 'AnnouncingController');
$this->resource('display-vote', 'DisplayVoteController');
$this->resource('announcing-supervisory-vote', 'AnnouncingSupervisoryVoteController');
$this->resource('supervisory-board-vote', 'SupervisoryBoardVoteController');
$this->resource('defense', 'DefenseController');
$this->resource('votiing-supervisory-board', 'VotiingSupervisoryBoardController');
$this->get('dashboard', 'DashboardController');
});
Because it is invalid. As you're using GET route, you must specify method name(unless you used ::resource):
$this->get('dashboard', 'DashboardController#methodName');
If you are using laravel 8 you need to add your controller and method name inside the array, otherwise, it will throw an error.
Route::get('/projects', User\ProjectController::class, 'index')->name('user.projects');
TO
Route::get('/projects', [User\ProjectController::class, 'index'])->name('user.projects');
I also face a similar problem:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'Frontend\FrontendController#index')->name('home');
Route::get('/post', 'Frontend\FrontendController#post')->name('post');
Route::get('/contact', 'Frontend\FrontendController#contact')->name('contact_us');
Route::group(['prefix' => 'admin'], function () {
Route::get('/create', 'Backend\BackendController#index');
//User Route
Route::get('/registration', '');
});
And I just remove the Route::get('/registration', ''); and it's work for me :)
try removes route cache file by
php artisan route:clear
Those who are new to Laravel or learning use
Route::resource('resource_name','controller_name')
to avoid this kind of error when you type:
php artisan route:list
In cmd or any other command line.
i think its because of :: before the class name instead use #
Route::get('/about','App\Http\Controllers\DemoController::about'); (Not working gives an error)
Route::get('/about','App\Http\Controllers\DemoController#about'); (But this statement works)
I hit the same issue but with a different cause. So I'm documenting here just in case someone else hits the same cause.
Specifically if you are using a Single Action Controller (ie: with __invoke), if you haven't added or omitted the correct use Laravel will hide the missing controller with "Invalid route action: [XController]."
This will fail
<?php
use Illuminate\Support\Facades\Route;
Route::post('/order', XController::class);
This will pass
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\XController;
Route::post('/order', XController::class);
I think its a bit unfortunate that Laravel masks the underlying issue, but I think it only applies to invokable controllers, even though its a silly mistake on my behalf.
Lastly if you set the route like below:
Route::post('example', PostController::class);
You should have an __invoke method in your controller.
For recent versions of laravel, try adding square brackets like
Route::post('login',[AuthController::class,'login'])
instead of
Route::post('login', AuthController::class,'login']).
The second route will throw the error Invalid route action: since you are not invoking the route. Another alternative you can add an __invoke function on your controller if you are using the second route as mentioned above.
Related
I wanted to use multi step registration. But got trouble at route. This is my first route.
Route::get('register-step2', [Auth\RegisterStep2Controller::class,'showForm']);
Route::post('register-step2', [Auth\RegisterStep2Controller::class, 'postForm'])
->name('register.step2');
but it got error
Target class [Auth\RegisterStep2Controller] does not exist.
so I change and mix with this code.
Route::group(['middleware' => ['auth']], function() {
Route::resource('roles', RoleController::class);
Route::resource('users', UserController::class);
Route::resource('products', ProductController::class);
Route::get('register-step2', [RegisterStep2Controller::class,'showForm']);
Route::post('register-step2', [RegisterStep2Controller::class, 'postForm'])
->name('register.step2');
But it says that: Target class [RegisterStep2Controller] does not exist.
How to use controller in auth in laravel 8 or 9.
I wanted to use registerstep2 controller in auth folder.
Make changes in your routes/web.php as per below
use App\Http\Controllers\Auth\RegisterStep2Controller;
...
Route::get('register-step2', [RegisterStep2Controller::class,'showForm']);
Route::post('register-step2', [RegisterStep2Controller::class, 'postForm'])->name('register.step2');
I'm having some trouble getting authentication working 100% in Laravel (all views seem to work so far except for "home") and was hoping to get some assistance from the Laravel experts out there.
A bit of background info:
PHP Version: 7.3.11
Laravel Version: 8.13.0
Used Composer to build the ui scaffolding (composer require laravel/ui)
Used the Bootstrap ui option (php artisan ui bootstrap --auth)
The issue
As mentioned above, I seem to be able to access all of the generated authentication views so far (login, register & the password reset views), however after registering with a dummy account I get the following error when trying to access the "home" view:
ReflectionException
Function () does not exist
The Stack trace is pointing to the following file:
"vendor/laravel/framework/src/Illuminate/Routing/RouteSignatureParameters.php:23":
<?php
namespace Illuminate\Routing;
use Illuminate\Support\Reflector;
use Illuminate\Support\Str;
use ReflectionFunction;
use ReflectionMethod;
class RouteSignatureParameters
{
/**
* Extract the route action's signature parameters.
*
* #param array $action
* #param string|null $subClass
* #return array
*/
public static function fromAction(array $action, $subClass = null)
{
$parameters = is_string($action['uses'])
? static::fromClassMethodString($action['uses'])
: (new ReflectionFunction($action['uses']))->getParameters();
return is_null($subClass) ? $parameters : array_filter($parameters, function ($p) use ($subClass) {
return Reflector::isParameterSubclassOf($p, $subClass);
});
}
/**
* Get the parameters for the given class / method by string.
*
* #param string $uses
* #return array
*/
protected static function fromClassMethodString($uses)
{
[$class, $method] = Str::parseCallback($uses);
if (! method_exists($class, $method) && Reflector::isCallable($class, $method)) {
return [];
}
return (new ReflectionMethod($class, $method))->getParameters();
}
}
With the following line (line 23) being highlighted as the error:
: (new ReflectionFunction($action['uses']))->getParameters();
And here are the routes used in the "web.php" file:
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome', ['pageTitle' => 'Home']);
});
Route::get('/services', function () {
return view('services', ['pageTitle' => 'Services']);
});
Route::get('/contact', function () {
return view('contact', ['pageTitle' => 'Contact']);
});
Auth::routes();
Route::get('/home', ['pageTitle' => 'Client Dashboard'], [App\Http\Controllers\HomeController::class, 'index'])->name('home');
After a bit of googling I've learnt that the method I'm trying to use to setup authentication has been deprecated and it is now advised to use Jetstream or Fortify, however I also found a few examples of people still managing to use this old method in their projects:
https://www.youtube.com/watch?v=NuGBzmHlINQ
As this is my first ever Laravel project I was really trying to just stick with the basics and not over complicate things for myself which is why I chose not to use Jetstream or Fortify and tried to stick to this older approach of setting up authentication. However I've been stuck on this for a couple of hours now and have not been able to figure out what's going wrong which is why I'm now seeking some help with it.
Happy to provide extra details/project code if needed - any help I can get with this would be really appreciated.
Btw, this also happens to be my first ever post on StackOverflow so any feedback on my question or advice on how I can improve it would also be greatly appreciated.
Cheers,
adb
Adjust your last route to include some type of 'action', by removing that second argument (as it only takes 2 arguments):
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
I have changed the web.php file.
I was getting the same error when using the following route:
Route::post('/delete/{id}',[AdminController::class],'destroyProduct');
Changing the route using this code fixed the problem:
Route::post('/delete/{id}','App\Http\Controllers\AdminController#destroyProduct');
I was getting the same error when making a simple route.
Route::get("/test", [ListPagesController::class, "index"]);
This was my code in the web.php file and I was routing my blade file in views via the controller.
When I later redirected it directly to web.php this way without using a controller, the problem was resolved.
Route::get("/test", function () {return view("main");});
I don't quite understand why but the problem is solved.
You Should add it in line 2 web.php
use App\Http\Controllers\YourControllerName;
For me, I changed:
Route::get('/branch/', [Controller::class, 'get_branch_list'])->name('branch');
To:
Route::get('/branch/', 'Controller#get_branch_list')->name('branch');
I am trying to add a new route to my application and can't seem to get it to work. I keep getting a 404 error. It looks like the physical path is looking at the wrong directory. Currently looking at D:\Web\FormMapper\blog\public\forms but should be looking at D:\Web\FormMapper\blog\resources\view\layout\pages\forms.blade.php
My request URL:
http://localhost/FormMapper/ /works fine
http://localhost/FormMapper/forms /doesn't work
http://localhost/FormMapper/forms.php /No input file specified.
my FormsController:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormsController extends Controller
{
public function index()
{
return view('layouts.pages.forms');
}
}
My web.php:
Route::get('/', function () {
return view('layouts/pages/login');
});
Route::get('/forms', 'FormsController#index');
My folder structure looks like this:
My config/view.php
return [
'paths' => [
resource_path('views'),
],
'compiled' => env(
'VIEW_COMPILED_PATH',
realpath(storage_path('framework/views'))
),
];
you must use dot for this. In your controller change to this:
return view('layouts.pages.forms');
If your route only needs to return a view, you may use the Route::view method. Like the redirect method, this method provides a simple shortcut so that you do not have to define a full route or controller. The view method accepts a URI as its first argument and a view name as its second argument. In addition, you may provide an array of data to pass to the view as an optional third argument:
Route::view('/', 'layouts.pages.login');
Route::view('/forms', 'layouts.pages.forms', ['foo' => 'bar']);
Check docs
After tracking digging deeper I determined that the issue was that IIS requires URL rewrite rules in place for Laravel to work properly. The index.php and '/' route would work b/c it was the default page but any other pages wouldn't. To test this I used the
php artisan serve
approach to it. and everything worked properly. Unfortunately I am unable to do this in production so I needed to get it to work with IIS.
I use the following middleware in routing Laravel:
Route::group(['prefix' => 'api/'], function() {
Route::resource('admin', 'adminController')->middleware('auth');
Route::resource('profile', 'profileController')->middleware('role');
});
I get this error when i call 'admin' or 'profile' path in URL
Use this
Route::prefix("/dashboard")->middleware(['first','second'])->group(function(){
});
It is because Route::resource() does not return anything. Its void. It doesn't return an object.
Laravel 5.4 - Illuminate\Routing\Router#resource
In Laravel 5.5 (in development), Route::resource() will be returning an object for fluently adding options to.
Simply reverse the order:
Route::middleware('scope:clock-in')->resource('clock', 'ClockController');
As lagbox stated:
Route::resource() does not return anything.
However middleware does.
Most likely your resource controller is not resolving to an actual controller. Some things to check
Check you actually have an adminController, and that the class name is in the correct case
Check that your controller is in the default namespace and, if not, change the controller namespace, or add a namespace attribute to your route
Check that your controller is not causing an exception on start that is being ignored resulting in you having a null controller.
In Laravel 8.x you can solve this problem with:
Route::group(['middleware' => 'auth','middleware' => 'role'], function () {
..........
});
I have set up my route as such:
Route::controller('clients', 'Controllers\ClientsController');
Through this method I can easily access all the controller functions via post and get. However I cannot test them as easily.
public function testCantDeleteOtherAccountsClient()
{
Route::enableFilters();
$user = Models\User::find(1);
$this->be($user);
$response = $this->action('GET', 'ClientsController#getDelete');
$this->assertRedirectedToAction('ClientsController#getIndex');
}
This test results in the message
InvalidArgumentException: Route [ClientsController#getDelete] not defined.
The method accessible via url though. What am I missing?
Just tried this out myself (a route specified via the controller) your issue is that using action requires a named route. Controller routes do not currently support this as far as I'm aware of.
If you create a test route:
Route::get('test', array(
'as' => 'testName',
'uses' => 'ClientsController#getDelete'
));
And try
$this->action('GET', 'testName');
The test should pass, you can view all the routes with names via php artisan routes.
You may want to use $this->client->request() instead. You can check if a redirect occurred with:
$this->assertRedirectedTo("some\url");
Note that $this->call() is just an alias to $this->client->request().
I found changing it to use call instead of action worked for me:
$response = $this->call('GET', 'clients/delete/1');