category controller not found laravel with resource route - laravel

I have a resource route for category controller
// category controller routes
Route::resource('api/category', CategoryController::class);
categorycontrooller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Category;
class CategoryController extends Controller
{
public function pruebas()
{
return "controlador de categoria";
}
public function index()
{
$categories = Category::all();
return response()->json([
'code' => 200,
'status' =>'success',
'categories' => $categories
]);
}
}
php artisan route:list
GET|HEAD api/category ..... category.index › CategoryController#index
testing it with postman I got a 404 not found
BadMethodCallException: Method App\Http\Controllers\CategoryController::show does not exist. in file /var/www/html/api-rest-laravel/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 68
I dont know what is hapenning.

You are getting the error because the Resource Controller seems to be incomplete.
You could either:
Create the resource controller. (https://laravel.com/docs/9.x/controllers#resource-controllers)
Route resource only for index. (https://laravel.com/docs/9.x/controllers#restful-partial-resource-routes)
To follow step 1:
Delete/Rename the existing file and run:
php artisan make:controller CategoryController --resource
To follow step 2:
Edit your route to look like:
Route::resource('api/category', CategoryController::class)->only([
'index'
]);

It's probably because you are trying to access the single category and that route uses show method in the controller which is not defined. Also it's better to use plural name for your resource routes:
Route::resource('api/categories', CategoryController::class);

Related

Invalid route action: [ProductController]

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.

send get request to specific file

I have a file called: test.php located in public/inc/test.php. Through jQuery, I can make a get request through jQuery ($.get), by typing:URL: "public/inc/test.php?" + param1` etc.
I want to do the same, but through Laravel. I want to have a route called: /sendparams/{param1}/{param2}, and do a get request to the test.php file from inside a route in web.php and pass the params variables.
How should the route be written?
edit:
by implementing the classes in laravel as external classes, this is no more needed.
you have in your laravel project a file named web.php inside the routes folder.
Define within the web.php file the below code:
Route::get('/sendparams/{param1}/{param2}', 'ControllerName#index');
Perform the below command to create the controller
php artisan make:controller ControllerName
Open ControllerName (app/Http/Controllers/ControllerName)
Your new controller has an structure like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MailController extends Controller
{
public function __construct()
{
}
/* This is the method executed in your route defined in web.php*/
public function index(Request $request) {
$param1 = $request->param1;
$param2 = $request->param2;
$allParams = $request->all(); // returns an associative array with all params
}
}
you can check your routes performed the below command
php artisan route:list
Regards

Laravel controller / store

I am new to laravel, i am trying to store a tweet to database and i need to insert user id, but this gives me an error
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tweet;
class TweetController extends Controller
{
public function store(Request $request){
return Tweet::create([ 'tweet' => request('tweet'), 'user_id' => Auth::id()]);
}
}
error i recive:
App\Http\Controllers\Auth' not found in file
/Applications/AMPPS/www/Twitter/Twitter/app/Http/Controllers/TweetController.php
Any idea?
Yes, you are missing the import, that's why it tries to find it in the Controller location, so put
use Illuminate\Support\Facades\Auth;
// or
use Auth; // you must have the Auth alias in the config/app.php array
as an import, or use the helper function auth()->id() instead.
So instead of mass-assigning the user, you can do the following, in your User model add this:
public function tweets()
{
return $this->hasMany(Tweet::class);
}
Then in your controller just do this:
auth()->user()->tweets()->create([ 'tweet' => request('tweet') ]);
You can use auth() helper to get user id:
auth()->user()->id

Laravel Controller not found

I'm learning laravel and I've tried to create a Controller.
I'm really new, so, please, elaborate.
I've used the following command to create the Controller
php artisan make:controller Api/EstadoController
So, EstadoController is under Controllers/Api
I also created a route at api.php
Route::namespace('API')->name('api.')->group(function() {
Route::get('/estados', 'EstadoController#index')->name('estados');
});
EstadoController has index function and correct namespace:
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class EstadoController extends Controller
{
public function index(){
return Estado::all();
}
}
Here is the error page:
Changing
API
to
Api
in my route resolved, like route creation.
Route:
Route::namespace('Api')->name('api.')->group(function() {
Route::get('/estados', 'EstadoController#index')->name('estados');
});
Please try to make route like this:
Route::get('/estados', 'Api\EstadoController#index');

Error on finding controller from Laravel API Router

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

Resources