When I hit my register route using Postman, it gave me the following error:
My api.php route:
Route::get('register', 'Api\RegisterController#register');
Here is my controller:
In the official Laravel 8 upgrade guide, you can see that controllers are no longer namespaced by default. This hinders the automatic class resolution with 'Controller#method' syntax.
See https://laravel.com/docs/8.x/upgrade#automatic-controller-namespace-prefixing
The new way of registering routes is to use the following syntax:
// routes/api.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\RegisterController;
Route::get('register', [RegisterController::class, 'register');
You can also adapt your RouteServiceProvider.php file to "re-enable" the old way to auto-load your controllers using the correct namespace with # syntax:
// App/Providers/RouteServiceProvider.php
public function boot()
{
// ...
Route::prefix('api')
->middleware('api')
->namespace('App\Http\Controllers') // put your namespace here
->group(base_path('routes/api.php'));
// ...
}
It should be changed to:
Route::get('register', [RegisterController::class, 'register']);
first, in the route add the controller name.
use App\Http\Controllers\RegisterController;
In the route use like this way
Route::get('register', [RegisterController::class, 'register']);
Related
already try different way, still not working with different error
anyone can help?
I just learn how to use laravel route and controller,
According to the Laravel routing documentation,
You have to pass the route action as an array:
Route::get('/', [ControllerRifan::class, 'index']);
You should make route like this
Route::get('/home', [HomeController::class, 'index']);
//Step 1
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
//
public function index(){
dd('stop');
}
}
// Step 2
<?php
use Illuminate\Support\Facades\Route;
// import controller here
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'index']);
Follow the instruction step by step it will work !!
enter image description here
I am having trouble to run my code on Laravel 8 routing with laravel-livewire.
The class is within Livewire\LandingPage.
The error I'm getting is
Attribute [livewire] does not exist
Here are my routes
<?php
use Illuminate\Support\Facades\Route;
Route::livewire('/' , 'LandingPage');
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
If you are using a recent install of Laravel 8, you will have Livewire V2. In this version, Route::livewire()has been removed. Instead, you specify a normal get() route, with the action being the Livewire component class.
Route::get('/' , App\Http\Livewire\LandingPage::class);
If you use livewire v1.x please use this annotation :
//(livewire v1.x)
Route::livewire('/post', 'LandingPage');
If you are using livewire v2.0 please use this one :
//(livewire v2.x)
Route::get('/post', \App\Http\Livewire\LandingPage::class);
From the error it appears you do not have the auth system setup:
Route::group(['middleware' => 'auth'], function () {
// Only with LiveWire v1
//Route::livewire('/blog', 'blog')->section('blog');
// For LiveWire 2.
Route::get('/blog' , 'BlogController#index');
});
You are calling the auth middleware and the error is stating there is no LoginController presently located in Auth\LoginController
Do you have any auth scaffolding setup?
Didn't realize this was such an old thread.
With Laravel 8.29 and LiveWire 2.4.0
UnexpectedValueException
Invalid route action: [App\Http\Controllers\App\Http\Livewire\Blog].
I think that is better tha you need create a new Controller in App\Http\Controllers and link the route with this Controller. In the view use #liveware to your LiveWire Controller.
Route::group(['middleware' => 'auth'], function () {
// Only with LiveWire v1
//Route::livewire('/blog', 'blog')->section('blog');
// For LiveWire 2.
Route::get('/blog' , 'BlogController#index');
});
App\Http\Controllers\BlogController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class BlogController extends Controller
{
public function index(){
return view('blog.index');
}
}
resources/views/blog/index.blade.php
#livewire('blog')
Note:
With the fix (https://laravel-livewire.com/docs/2.x/upgrading)
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace) // Remove me
->group(base_path('routes/web.php'));
}
you will have problems with routes in Middlewares
Illuminate\Contracts\Container\BindingResolutionException Target class
[Auth\LoginController] does not exist.
I'm trying to use simple laravel api for getting and sending requests, after define this api routes in api.php:
Route::prefix('Api/v1')->group(function () {
Route::any('login', 'Api\v1\AuthController#login');
Route::any('register', 'Api\v1\AuthController#register');
});
and creating AuthController in app/http/controller/Api/v1 directory:
class AuthController extends Controller
{
public function login()
{
dd(request()->all());
}
public function register()
{
dd(request()->all());
}
}
i get 404 error on this link:
http://127.0.0.1:8000/Api/v1/login
how can i resolve this problem?
Routes in api.php are automatically prefixed with /api. Currently, your routes are:
http://127.0.0.1:8000/api/Api/v1/login
http://127.0.0.1:8000/api/Api/v1/register
So navigating to http://127.0.0.1:8000/Api/v1/login is a 404.
If you remove /Api, and just use Route::prefix('/v1') ... then you should have no issue.
Also, always double check your routes with php artisan route:list to see what's wrong.
The API Routes are already prefixed by /api . I think the correct structure you'd looking for would be
Route::prefix('v1')->group(function () {
Route::any('login', 'AuthController#login');
Route::any('register', 'AuthController#register');
});
This way, you're calling the methods Login and Register from you /Controllers/AuthController file with the route
http://127.0.0.1:8000/api/v1/login
You can use many ways to define routes for API in laraval > routes > api.php file.
In this i'm going to explain how we can use routes group in the laraval..
Route::group([
'namespace' => 'Customers', //namespace App\Http\Controllers\Customers;
'middleware' => 'auth:api', // this is for check user is logged in or authenticated user
'prefix' => 'customers' // you can use custom prefix for your rote {{host}}/api/customers/
], function ($router) {
// add and delete customer groups
Route::get('/', [CustomerController::class, 'index']); // {{host}}/api/customers/ this is called to index method in CustomerController.php
Route::post('/create', [CustomerController::class, 'create']); // {{host}}/api/customers/create this is called to create method in CustomerController.php
Route::post('/show/{id}', [CustomerController::class, 'show']); // {{host}}/api/customers/show/10 this is called to show method in CustomerController.php parsing id to get single data
Route::post('/delete/{id}', [CustomerController::class, 'delete']); // {{host}}/api/customers/delete/10 this is called to delete method in CustomerController.php for delete single data
});
You can create controller using artisan command with default methods
php artisan make:controller Customers/CustomerController --resource
Trying to use invokable controllers, but it seems to fail to find the __invoke method?
Invalid route action: [App\Http\Controllers\App\Http\Controllers\MainController].
It seems to be returning true on:
if (! method_exists($action, '__invoke')) {
throw new UnexpectedValueException("Invalid route action: [{$action}].");
}
Routes:
<?php
Route::get('/', \App\Http\Controllers\MainController::class);
MainController:
<?php
namespace App\Http\Controllers;
class MainController extends Controller
{
public function __invoke()
{
dd('main');
}
}
Laravel by default assumes that your controllers will be located at App\Http\Controllers\. So when you're adding the full path to your controller, Laravel will check it there, at App\Http\Controllers\App\Http\Controllers\MainController.
To solve it simply remove the namespace when you're registering the route, and register it like this:
Route::get('/', MainController::class);
Alternatively, you can stop this behavior by removing ->namespace($this->namespace) from mapWebRoutes() method on RouteServiceProvider class, which is located at App\Providers folder. Then you can register your routes like this:
Route::get('/', \App\Http\Controllers\MainController::class);
Alternatively, you can use:
use App\Http\Controllers\MainController;
Route::get('/', [MainController::class, '__invoke']);
In this case, the namespace provided in RouteServiceProvider won't be taken into account.
The advantage of this is that now your IDE will be able to reference the class usage and you can navigate by clicking on it.
The best answer that works for everyone is laravel documentation.
just use this at the top of your route(web.php) if (websiteController is the name of your controller)
use App\Http\Controllers\WebsiteController;
and define your route like this for your index page
Route::get('/', [WebsiteController::class, 'index']);
take note of the
[ ]
You have to create the crontoller with argument "--invokable"
php artisan make:controller YourController --invokable
Always Declare/Use in Top
use App\Http\Controllers\backend\DashboardController;
Then Use this way
Route::get('/dashboard', [DashboardController::class, 'dashboard'])->name('dashboard');
If have Auth and Use Group
Route::middleware([
'auth:sanctum',
config('jetstream.auth_session'),
'verified'
])->group(function () {
Route::get('/dashboard', [DashboardController::class, 'dashboard'])->name('dashboard');
})
Change your route to:
Route::get('/', "MainController");
In my case I forget to set #action, so change your code from:
Route::get('admin/orders', 'Admin\OrderController')->name('admin.orders');
to:
Route::get('admin/orders', 'Admin\OrderController#index')->name('admin.orders');
you have mentioned get link, but you have not declared which method it should call.
Route::get('/', \App\Http\Controllers\MainController::class);// if you are importing lass like this you have to use resource instead of get.
you can solve this issue by two ways,
first way,
Route::get('/', '\App\Http\Controllers\MainController#index'); // you have to mention your method which you have mentioned in controller
another way is,
Route::resource('/', \App\Http\Controllers\MainController::class);
In, 2nd method laravel will automatically find which request and where should redirect.2nd option is prefered if you are using multiple method for the same route.
Route::resource('/', \App\Http\Controllers\MainController::class);
Use method 'resource'
I created a multi auth in Laravel 5.3,
Then moved Controller/Auth/[files] to:
Admin: Controller/Admin/Auth/[files] &
Site: Controller/Site/Auth/[files]
In command line I type php artisan route:list,
It shows me following error:
Class App\Http\Controllers\Auth\LoginController does not exist
Where is my problem?
You need to manually define all the Auth routes in web.php and remove Auth::routes().
just define all your routes like,
Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function () {
Route::get('/', 'Auth\LoginController#showLoginForm');
Route::post('login', 'Auth\LoginController#login');
Route::post('logout', 'Auth\LoginController#logout');
});
The two default authentication controllers provided with the framework have been split into four smaller controllers. The easiest way to upgrade your application to the new authentication controllers is to grab a fresh copy of each controller from GitHub and place them into your application.
https://github.com/laravel/laravel/tree/5.3/app/Http/Controllers/Auth
You should also make sure that you are calling the Auth::routes() method in your routes/web.php file. This method will register the proper routes for the new authentication controllers.
Pasted this answer from the Laravel upgrade documentation.
If you're moving controllers to a custom directory, you shouldn't use auth routes. So remove this from routes file:
If you're using 5.2
Route::auth();
If you're using 5.3
Auth::routes();
And then build auth routes manually.
In my case, I had the same issue when I type: php artisan route:list
As I am using Laravel 8.x, I noticed there is a tiny change in the notations which I had to apply in routes/web.php in 2 steps:
use App\Http\Controllers\LoginController; // step 1
Route::post('/login', [LoginController::class, 'login']); // step 2
Hello please check your route/web.php
/* For get login page*/
Route::get('/login', function () {return view('auth.login');});
/* while post remember to user Auth\controllername so you can get the perfect path for the custom login */
Route::post('/login', 'Auth\LoginController#authentication')->name('login');
It Happens With Me Now And I Had Solved It By Another Way
Simply Copy The Auth Folder
And Put It In The Path Of Admin Controllers Folder
And Open Each File And Change
namespace App\Http\Controllers\Auth;
To
namespace App\Http\Controllers\Dashboard\Auth;
Hope It Just Helps Somebody
for me just i'am add Auth::routes(); in my routes file routes/web.php or any other routes file like routes/admin.php if you create it
add this
namespace App\Http\Controllers;
Download or copy RegisterController.php from another project.
Paste it in your project under
Controllers/Auth/[files]
That's it