Attribute [livewire] does not exist - php

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.

Related

i got some error in php 8.2.0 laravel 10.0.0 . Invalid route action: [App\Http\Controllers\ControllerRifan]

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

Target class controller [controllerName] not found in Laravel 8

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

using Laravel 7 Api routes

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

Laravel Target class [App\Http\Controllers\App\Http\Controllers\ApiController] does not exist

For some reason, which is probably my fault, Laravel thinks it should be looking for the class ApiController in path: 'App\Http\Controllers\App\Http\Controllers', so... it doubles, but I have no idea why.
It's a brand new Laravel 6 project, I've created the ApiController with the make:controller artisan command and added a function, like this:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ApiController extends Controller
{
public function base() {
return 'This is a test function';
}
}
Then I've added a route to the api routes like this:
use App\Http\Controllers\ApiController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::group(['prefix' => '/v1', 'as' => 'api'], function () {
Route::get('/base', ['uses' => ApiController::class . '#base'])->name('base');
});
As you can see, I've even 'imported' the controller, but it just can't find it.
That's it, no other files or changes to the project. Also tried clearing route cache and dump-autoload, but that did not change anything.
In my case problew was, in RouteServiceProvider, in using routes Namespace
protected $namespace = 'App\Http\Controllers';
In Laravel 8 namespace was commented out, i remove namespace from chain, because my web routes not fully moved to Laravel 8 syntax and i need this namespace.
Route::prefix('api')
->middleware('api')
-̶>̶n̶a̶m̶e̶s̶p̶a̶c̶e̶(̶$̶t̶h̶i̶s̶-̶>̶n̶a̶m̶e̶s̶p̶a̶c̶e̶)̶
->group(base_path('routes/admin-api.php'));
If you wanna ::class reference in the router, it should be done like this.
Route::group(['prefix' => '/v1', 'as' => 'api'], function () {
Route::get('base', [ApiController::class, 'base'])->name('base');
});
This should work:
Route::group(['prefix' => '/v1', 'as' => 'api'], function () {
Route::get('base', 'ApiController#base')->name('base');
});
No need to add the "use", since controllers are referenced from the App/Controllers namespace, as you can corroborate on the RouteServiceProvider.
The syntax of your route is a combination of "old syntax" vs "new syntax"
What you are trying to achieve is:
Route::get('/base', [ApiController::class, 'base'])->name('base');
Either remove this line:
use App\Http\Controllers\ApiController;
or add a \ to the start:
use \App\Http\Controllers\ApiController;
In my case (Laravel 8 project), I needed a separate route for destroy, because deleting didn't use html form, so my web.php file is like:
use App\Http\Controllers\LocationController;
...
Route::resource('/locations', LocationController::class);
Route::get('/locations/destroy/{location}', [LocationController::class, 'destroy']);
But in that case if I put use App\Http\Controllers\LocationController the first line (Route::resource...) fails, if I remove it then the second line fails. So I removed use line and added App\Http\Controllers into the second line:
Route::resource('/locations', LocationController::class);
Route::get('/locations/destroy/{location}', [App\Http\Controllers\LocationController::class, 'destroy']);
So obviously Laravel doesn't automatically add App\Http\Controllers in the second form of Route.
I got this error when in resource controller description me pasted one from fresh project:
Route::resources([
'my_url' => LisseyDoruHisobotController:class,
..., //other controllers
]);
as being a recommended signature in Laravel 8 , but am currently busy on 7 or 6 version, where should be:
Route::resources([
'my_url' => 'path\to\LisseyDoruHisobotController',
..., //other controllers
]);
otherwize it will show doubled path
Laravel Target class [App\Http\Controllers\App\Http\Controllers\ ] does not exist

Laravel: Not picking up __invoke method?

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'

Categories