I have my view as follow resources/views/shop/index.blade.php
I have a controller named ProductController.
In the route, I have the following
Route::get('/', [
'uses' => 'ProductConroller',
'as' => 'product.index'
]);
There is an error;
ProductController` is not invokable.
The controller class ProductController is not invokable. Did you forget to add the __invoke method or is the controller's method missing in your routes file?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
//
public function getIndex()
{
return view('shop.index');
}
}
Please, I don't know what to do else. I have tried everything. Laravel 8
I finally got it by using
use App\Http\Controllers\ProductController;
Route::get('/', [ProductController::class, 'getIndex']);
It is funny though, it took me several hours to figure what is needed to be done.
Related
The routing process has changed in Laravel version 8. I did as in the internet but it gives an error. Where am I doing wrong?
Route file
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Backend\BackendHomeController;
Route::get('/', function () {
return view('welcome');
});
Route::get('/admin', [BackendHomeController::class, 'index'])->name("index");
Controller file
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public static function index()
{
return view("backend.index");
}
}
BackendHomeController also like this. Everything seems to be correct, but am I doing something wrong with using?
Error says
target class does not exist error
Class is referenced as BackendHomeController but your file is named HomeController. These should align for autoloading to be working.
Can Someone please help me on laravel postcontroller? I am using Laravel 8 but I am seeing below error though my Postcontroller is existing on app>Http>Controllers>PostController.php. Below is my code and I appreciate yoenter image description hereur help in advance.
Routes
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostsController;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route:: get('/p/create','PostsController#create');
Route::get('/profiles/{users}',[App\Http\Controllers\HomeController::class, 'index'])->name('home');
PostController
use Illuminate\Http\Request;
class PostsController extends Controller {
public function create() {
return view ('posts/create');
}
}
Insead of :
Route:: get('/p/create','PostsController#create');
use :
Route:: get('/p/create',[PostsController::class , 'create']);
You can read more about laravel 8 routes here:
https://laravel.com/docs/8.x/routing#the-default-route-files
I have installed jwt authentication & I have created a controller i.e., AuthController Inside Api Directory. I have defined the in routes/api.php as:
Route::group(['prefix'=>'v1', 'namespace' => 'Api'],function($app){
Route::get('/test', function(){
return "HEllo";
});
Route::get('test', 'AuthController#test');
});
When I hit the url as: http://localhost:8000/api/v1/test then I am getting error as Class Cotrollers\Api\AuthController does not exist.
AuthController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class AuthController extends Controller
{
public function test() {
return "Hello";
}
}
RouteServiceProvider.php:
Route::prefix('api')
// ->middleware('api')
// ->namespace($this->namespace) ->group(base_path('routes/api.php'));
Uncomment the ->namespace($this->namespace) line.
In your Route::group statement you have defined the namespace of the route group as 'Api'.
But the AuthController resides in the App\Http\Controllers namespace, and not the Api namespace.
To fix this add an Api namespace in your App\Http\Controllers and refer it there (best practice is creating a directory in the Controllers directory named Api so the directory structure follows the namespace):
AuthController.php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class AuthController extends Controller
{
public function test() {
return "Hello";
}
}
Here you need to make changes to,
App\Providers\RouteServiceProvider.php
In the RouteServiceProvider.php add
protected $namespace = 'Path\To\Controllers';
Like:
protected $namespace = 'App\Http\Controllers';
Thats it!
Please let me know if this solved your problem.
Change the Auth controller namespace definition to:
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
// you need to use your controller top of the api.php file
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
});
I created a fresh Laravel framework.
I created a controller named PostsController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\Http\Controllers\Controller;
class PostsController extends Controller
{
public function index()
{
$posts = Post::get();
return response()->success(compact('posts'));
}
}
Then I created a route in the file api.php:
Route::get('posts', 'PostsController#index');
I ran the command
$ php artisan serve`
and I tested the URL
localhost:8000/api/posts
This error occurs:
BadMethodCallException
Method Illuminate\Routing\ResponseFactory::success does not exist.
file: vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php
line: 100
throw new BadMethodCallException("Method {$class}::{$method} does not exist.");
I can't understand why this happened. Please help me.
There is no success method on the ResponseFactory. You can find the available methods here.
You are calling a macro function that is not registerd to the responseFactory.To use success method,Create your custom responseServiceProvider and write this inside boot()
Response::macro('success',function($data){
return Response::json([
'data'=>$data,
]) ;
});
And then register your ResponseServiceProvider to app.php by adding your Class name to the array called providers. This is how you add to the array
App\Providers\ResponseMacroServiceProvider::class
just trying to get to grips with the basics of Laravel. I was getting syntax errors in my areasController file. Once they were resolved I started recieving this error: ReflectionException in Route.php line 280:
Class App\Http\Controllers\areasController does not exist. So it seems like Laravel can find the file to know that when there are errors in it, but not the rest of the time. Any help appreciated, this is my first framework so I'm pretty stumped.
routes.php:
Route::get('/', function () {
return view('welcome');
});
Route::get('locations', function() {
return view('locations');
});
Route::get('areas', ' areasController#areas');
areasController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
class areas extends Controller
{
//
public function areas() {
$areas = DB::table('areas')->all();
return view('areas');
}
}
Any help would be appriciated.
In your routes.php file, you ask to use the method areas from areasController but in your controller file, you define class areas extends Controller
It should be class areasController extends Controller then it should work