I would appreciate any help you could give me with the following problem I have.
I am trying to implement a Single Page Application (SPA) with Laravel and Vue, at the moment I am defining the routes, I have something like the following:
routes/app.php
Route::group(['middleware' => 'web'], function () {
Route::any('{path}', function () {
return view('index');
});
});
What I'm trying to do here is the following:
For the middleware web you will only have one view available, in this case in the file index in (resources/views/index.php)
I want that regardless of the method used, Laravel returns the view I want. But I have not succeeded.
Output of php artisan route:list
And when I try to verify with Postman I receive a 404 in response regardless of the method or the URL that I requested .. what could I be doing wrong? Thank you very much in advance.
EDIT 1
I have modified my router and added a conditional as follows:
Route::group(['middleware' => 'web'], function () {
Route::any('{path}', function () {
return view('index');
})->where('path', '.*');
});
Now the GET and HEAD method work properly, however ... when I try the POST method, I get the following error ..
EDIT 2
As mentioned #lagbox in the comments below, error 419 refers to missing CSRF token. I could disable this check in the file Middleware/VerifyCsrfToken.php.
However I wish I could just return the desired view .. without first checking the CSRF token. Another detail that I find is the following:
When in Postman I make a request by a method different from the typical methods ... let's say UNLINK method, I get the following result:
Which leaves me a bit confused, when I define in my routes file web.php Route::any, does it mean any route or not?
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.
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.
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 installed Laravel and set up authentication and I have also created a route group like this:
// users that want to access test route should be logged in.
Route::group(['middleware' => ['web', 'auth']], function () {
Route::get('first', function () {
return 'first';
});
});
The problem is when I access the route like this:
http://localhost/first
I can see my "first" message, but when I refresh the same page laravel redirects me to:
http://localhost/home
I could not solve this and I have moved my first route out of the route group now everything is working well. If I keep it in the route group with auth & web middlewares it is not working.
Try to remove web middleware if you're using 5.2.27 and higher.