Yii2 Adding new console controller - php

Why yii2 throw exception, when I try use console controller? Code:
<?php
namespace app\commands;
use yii\console\Controller;
class Hashtag extends Controller
{
public function actionIndex($search = 'test')
{
echo $search;
}
}
Controller located in: app\commands\HashtagController
When using terminal: php yii hashtag
Exception 'yii\base\UnknownClassException' with message 'Unable to find 'app\commands\HashtagController' in file: /var/www/html/yiitask/yii2/commands/HashtagController.php. Namespace missing?'
in /var/www/html/yiitask/yii2/vendor/yiisoft/yii2/BaseYii.php:291
Other controller in this folder that was created before, working well.

Your namespace is wrong set the namespace for console controller properly eg: (depend where you have you console controller directory)
namespace app\console\controllers;
then could be is missing controller
class HashtagController extends Controller
{
instead of
class Hashtag extends Controller
{

Related

Fatal Error Class 'App\Http\Controllers\Controller' not found Laravel 5.4

I tried to route with using name .
I already tried to solve with using Composer update , Clear Cache , Generate App key.
But it's showing fatal error .
Error View Message :
(1/1) FatalErrorException
Class 'App\Http\Controllers\Controller' not found
in ClassesController.php line 13
My Route Code Below :
<?php
Route::prefix('Classes')->group(function(){
Route::get('/add','ClassesController#index')->name('AddNewClass');
});
My Controller Code Below :
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ClassesController extends Controller{
public function index()
{
return "Method Access";
}
In this event, I would first ensure that the file containing the class Controller exists in the same directory with the same namespace as the ClassesController, as implied in your code.
If the Controller does not exist, you can either create the class or simply remove the extends Controller and the line use App\Http\Controllers\Controller; from your code.
its worked for me.
usercontroller
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Show the profile for the given user.
*/
/* http://localhost:8080/laravelapps/blog/public/staff */
public function showProfile()
{
return 'new';
}
}
web.php
Route::get('/staff', 'UserController#showProfile');
http://localhost:8080/laravelapps/blog/public/staff
Check whether Controller.php file exists or not in your Controllers folder if not exists download or put from laravel project.
After that run following command php artisan app:name newproject
Make sure in the above line i have updated text 'newproject' that should be first line of your Controller.php file. For example: namespace newproject\Http\Controllers;

FatalThrowableError Class 'App\Models\Patient' not found in laravel 5.4

I have created a model by using the command php artisan make:model Patient. The command creates a model named Patient in the app folder.
In Model Patient:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Patient extends Model
{
//
public function getPurchaseOrder(){
return "Hello World";
}
}
In Controller PatientController:
namespace App\Http\Controllers;
class PatientController extends Controller
{
protected $patientModel;
public function __construct(Request $request, PatientInterface $patient)
{
parent::__construct($request);
$this->patientModel = new \App\Patient();
}
public function test(){
echo $this->patientModel->getPurchaseOrder();
}
}
It's working fine. The problem is when I create a folder named Models inside the app folder and move Patient model then call the model function it gives an error:
FatalThrowableError Class 'App\Models\Patient' not found
Any help will be appreciated.
When you move a class to a different directory, for it to be loaded by composer with the PSR-4 standard, you must also update the class' namespace to match.
namespace App\Models;
In addition, when you run the make command, you can include a namespace in that to automatically put it in the directory with the correct namespace:
php artisan make:model Models\\Patient

Model Class not found error

Using Laravel 5.4, I am getting this error on view whenever I call my method it shows on screen
(1/1) FatalThrowableError
Class 'App\Models\Chat\User' not found
Hierarchy of my project :
Controllers
- ChatMessageController
Models
-Chat
-message.php
-User.php
and here's my controller class code :
namespace App\Http\Controllers\Chat;
use App\Http\Controllers\Controller;
use App\Models\Chat\Message;
use App\Models\User;
use Illuminate\Http\Request;
class ChatMessageController extends Controller
{
public function index()
{
$messages = message::with(['user'])->latest()->limit(100)->get();
return response()->json($messages,200);
}
}
In message.php, probably you forgot to add: use App\Models\User;.
So, it is trying to find User in the wrong space.

how to make a directory in controller - laravel

I created a Panel directory inside Controller directory .
there is a login function inside AdminController.php
class AdminController extends Controller
{
//
public function login()
{
return 'test';
}
}
in routes.php I wrote a route like this:
Route::get('/cp/login','Panel\AdminController#login');
but when I run below url I got some errors that there isn't exist this controller :
http://localhost:8000/cp/login
ReflectionException in Route.php line 280: Class
App\Http\Controllers\Panel\AdminController does not exist
Try adding the appropriate namespace to the top of the AdminController file, you will also need to specify the namespace for the Controller class that it extends, as they are under different sub-namespaces.
You can read more about PSR-4 autoloading here http://www.php-fig.org/psr/psr-4/.
Based on the directory structure that you have there it should read
<?php
namespace App\Http\Controllers\Panel
use App\Http\Controllers\Controller;
class AdminController extends Controller {
//..
}
you should add the namespace to the contorller
Change you namespace to
namespace App\Http\Controllers\Panel;
Laravel will resolve controllers based on your name spacing, not on your directory structure.

Laravel 5: Using a controller in a view composer - controller class not found

I followed these instructions and created a view composer for my default layout.
My DefaultComposer.php is located under app/Http/ViewComposers, hence the namespace used below:
<?php namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
class DefaultComposer {
public function compose(View $view) {
$data['language'] = LanguageController::getDefaultLanguage();
$view->with($data);
}
}
?>
Now, when I load a page, I get the following error:
Class 'App\Http\ViewComposers\LanguageController' not found
This happens because the LanguageController.php is placed under app/Http/Controllers, which is a different namespace.
How can I use the LanguageController class in my DefaultComposer?
Update:
Using this declaration:
use App\Http\Controllers\LanguageController as LanguageController;
throws: Class 'App\Http\Controllers\LanguageController' not found. I'm confused.
I figured this out. As the controllers in my application live in the global namespace, all I needed to do is add a backslash in front of the class name.
So instead of this:
$data['language'] = LanguageController::getDefaultLanguage();
I did this:
$data['language'] = \LanguageController::getDefaultLanguage();

Categories