Laravel Dingo class does not exist - laravel

I have code on api.php thats using a Dingo function and I am having trouble calling the controller because it always state that Controller is not found even though in their documentation it clearly has specified that I have to put the full path of the controller in which what is I have done.
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
// use Dingo\Api\Contract\Http\Request;
// use Dingo\Api\Facade\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
// This Will Work
// $api->get('hello', function() {
// return "hi";
// });
// Will not work
$api->get('hello', 'App\\Api\Controllers\\TestController#index');
$api->get('hi','App\\Http\\Controllers\\TestController#index');
});
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
File locations:
Error Message: Error Message: message: "Target class [app\Http\Controllers\TestController] does not exist.", status_code: 500,
What do you think I am missing here?

Casing really does matter when referencing files. I just found this out recently.
Inside hello route
namespace app\Api\Controllers;
use Dingo\Api\Http\Request;
use app\Http\Controllers\Controller;
class TestController extends Controller
{
public function index(Request $request)
{
return "hi";
}
}
Have to change the app\Http\.. to App\Http\...
Also did the same on Api.php Controllers Path
Just a reminder so when you try to use "Copy relative path" on vscode try to watch out of the naming convention.

Related

larvael form submit to database giving error

i am new to laravel and i am trying to submit form into data base but i am getting error i dont know why
i have added the screen shot along with that controller
when i do dd($REQUEST->all()) i am getting the form data
<?php
namespace App\Http\Controllers;
use App\Inventory;
use Illuminate\Http\Request;
class InventoryController extends Controller
{
public function index(){
return view('invetory.index');
}
public function sales(){
return view('invetory.sale');
}
public function create(Request $REQUEST){
// dd($REQUEST->all());
inventories::Create($REQUEST->all());
}
}
web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('/inventory', 'App\Http\Controllers\InventoryController#index')->name('map');
Route::get('/inventory/sales', 'App\Http\Controllers\InventoryController#sales');
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::post('/inventory', 'App\Http\Controllers\InventoryController#create')->name('invetory.create');
according to your code the controller should look like
<?php
namespace App\Http\Controllers;
use App\Models\Inventory;
use Illuminate\Http\Request;
class InventoryController extends Controller
{
public function index(){
return view('invetory.index');
}
public function sales(){
return view('invetory.sale');
}
public function create(Request $request){
Inventory::Create($request->all());
}
}
change the namesapce of inventory to use App\Models\Inventory;
and also , inside create function use :
Inventory::Create($request->all());

Target class [PostController] does not exist. but it dose

The error I am getting is: Target class [PostController] does not exist but it does.
Route web.php
Route::get('/post', 'PostController#index');
Route::post('/post', 'PostController#store');
Route::get('/', function () {
return view('create');
});
PostController.php
namespace App\Http\Controllers;
use App\Post;
use Redirect,Response;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
return view('create');
}
public function store(Request $request)
{
$data = json_encode($request);
Post::create($data);
return back()->withSuccess('Data successfully store in json format');
}
}
This error comes in Laravel new version because there is no namespace prefix being applied to your route groups that your routes are loaded into. In the old version of Laravel, the RouteServiceProvider contained a $namespace property which would automatically be prefixed onto the controller route.
To solve this, you either can go to RouteServiceProvider and uncomment the line:
protected $namespace = 'App\\Http\\Controllers';
Or you can use closure-based syntax:
use App\Http\Controllers\PageController;
Route::get('/page', [PageController::class, 'index']);
Another way would be to use the fully qualified class names for your Controllers:
Route::get('/page', 'App\Http\Controllers\PageController#index');
use this line on the top of the (web.php) maybe your problem will resolve
use App\Http\Controllers\PostController;

Laravel Seperate folders for Web and API Controllers causing error

Im currently trying to create an API for my laravel project,
I have decided to move my API controllers into a subfolder of Controllers. This is the folder structure:
This is the routes file for the api:
<?php
use Illuminate\Http\Request;
use app\Http\Controllers\APIControllers;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::group(['prefix' => 'v1'], function() {
Route::get('/event_locations', 'EventLocationsAPIController#all');
});
And this is the EventLocationsAPIController:
<?php
namespace App\Http\Controllers\APIControllers;
use App\EventLocation;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class EventLocationAPIController extends Controller
{
public function all()
{
$locations = EventLocation::all();
return response()->json($locations);
}
}
When I send an GET request to /api/v1/event_locations I get the following error
Target class [App\Http\Controllers\EventLocationsAPIController] does not exist.
Any help would be appreciated!
You need to declare the namespace in route group as well.
Route::group(['prefix' => 'v1','namespace'=>'APIControllers'], function() {
Route::get('/event_locations', 'EventLocationAPIController#all');
});
you have given EventLocations plural and the controller name is singular EventLocation change the name of the controller as EventLocationAPIController in the route file.
You have to declare your nameaspace on router's group like this:
Route::namespace('APIControllers')->group(function () {
// Controllers Within The "App\Http\Controllers\APIControllers" Namespace
Route::group(['prefix' => 'v1'], function() {
Route::get('/event_locations', 'EventLocationAPIController#all');
});
});
Check Laravel docs for more info.

Non-static method Illuminate\Routing\Route::middleware() should not be called statically

I have encountered this error in this file : route/api.php
This error refers to line 16
<?php
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
16- Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request::user();
});
Route::post('login', 'Api\AuthController#login');
Problem solved by changing this line
from
use Illuminate\Routing\Route;
to
use Illuminate\Support\Facades\Route;
Since You've not defined in Your question what is error message I can only predict that You're using middleware incorrectly.
Middleware should be assigned to group.
Route::middleware('auth:api')->group(function() {
Router::get('/user', function (Request $request) {
return $request::user();
});
});
or You've to use middleware after defining the route handler:
Route::get('/user', function(Request $request) {
return $request::user();
})->middleware('auth:api');
Because the method middleware is not static method, according to the assigning-middleware-to-routes try it like this:
Route::get('/user', function (Request $request) {
return $request::user();
})->middleware('auth:api');

Laravel 5 Auth::user() is empty everywhere except index() function and blade template

I've been trying for some time to figure this out, but no solution I found was conclusive. Basically I'm trying to retrieve a list of clients based on the user logged in, but I cannot retrieve the user object anywhere in the controller except the index() function where is returns the view, and also the blade template that displays the logged in user's name.
This is my ClientController.php:
<?php
namespace App\Http\Controllers;
use App\Http\Models\Client;
use App\Http\Requests\GetClientsRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class ClientController extends Controller
{
protected $user;
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->user = Auth::user();
return $next($request);
});
}
/**
* Show the clients page.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
Log::info("client user: " . print_r($this->user, 1));
return view('page');
}
public function getClients()
{
$currentUser = Auth::user();
if ($currentUser) {
$clients = Client::with('account')
->where('user_id', $currentUser->account)
->get();
$collection = collect($clients);
return response($collection->toArray());
}
}
}
The Log in the index function prints out the user object no problem - but when it's called in the getClients() function, it's empty. I also tried using this in the __construct():
$this->middleware('auth');
As per the Laravel template I was using, but whenever I call the getClients API route I always get a 401 Unauthorized error.
Here is my api.php routes file (although only currently using the getClients call) :
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('welcome-message', 'DashboardController#getWelcomeMessage');
Route::prefix('clients')->group(function () {
Route::get('/', 'ClientController#getClients');
Route::get('/{clientId}', 'ClientController#getClient');
Route::post('/', 'ClientController#postCreateClient');
Route::put('/{clientId}', 'ClientController#putUpdateClient');
});
And my web.php routes file:
<?php
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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!
|
*/
Auth::routes();
Route::get('/', 'HomeController#index')->name('dashboard');
Route::get('/clients', 'ClientController#index')->name('clients');
The system knows I'm logged in, because I wouldn't even be able to visit the client dashboard at all, so I don't understand why I cannot get a result from Auth::user() in the function - unless I'm using the middleware incorrectly?
You will have to use the auth:api middleware to allow API authentication.
To do so, add this middleware to your API route, omit in that case to add middleware in the __constructor.
Route::group([
'prefix' => 'clients',
'middleware' => ['auth:api']
], function(){
Route::get('/', 'ClientController#getClients');
// ... etc
});
Make sure that you have a valid access token to access your API. When using your API with a JS front-end, read about it in the Laravel docs.
Note that 'web' authentication (middleware: auth) uses a different mechanism than API authentication (middleware: auth:api) to authenticate users.

Resources