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.
Related
i recive this error
"\Controllers\ProductController does not exist "
but indeed i have this controller in my app/http/controller
class ProductController extends AdminController {
public function index() {
$products=Product::latest()->paginate(20);
return view('admin.product.index',compact('products'));
}
}
.....
Route::get('/admin/product','Productcontroller#index');
maybe check your namespace and see if changing that directory suited with the error will help.
namespace App\Http\Controllers\admin
Directs to the Admin Folder in the Controllers Folder
use App\Http\Controllers\Controller
Uses the main Controller.php file
After all this you can check your routes if they work at this point.
If you want to extend another controller instead of the main controller like in your case, I think you should check the directory of the AdminController.php and use the Use command again if the directory isn't in your namespace directory.
namespace App\Http\Controllers
use App\Http\Controllers\admin\AdminController
Atleast that's where i think the extent of your problem is.
This line:
Route::get('/admin/product','Productcontroller#index');
should probably be (watch the case):
Route::get('/admin/product','ProductController#index');
Also make sure that you import the right namespace: Is your controller really living in App\Http\Controllers namespace ?
I am very new in Laravel and this is my first project. I have this problem and I don`t know how to fix it.
This is my directory
I am sure I have tried all I know
This is my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class EditareProfilController extends Controller
{
public function __construct()
{
$tasdhis->middlewasdare('auasdth');
}
public function index()
{
return vasdasdiew('editaasdasdasdeprofil');
}
}
and this is my route:
Route::get('/editarasdasdeprofil', 'EditareProfilasdasdasdontroller#index')->name('editarepasdasdasdrofil');
The default location for controllers is app\Http\Controllers if you add a folder just add it to your route in order to laravel localize it. In your code you are storing your controller under the Auth folder.
Update your route to:
Route::get('/editareprofil', 'Auth\EditareProfilController#index')->name('editareprofil');
And also don't forget to update the namespace.
namespace App\Http\Controlllers\FOLDER;
Where to save new model file created in laravel 5.3 in /app/http folder or in /app folder
And how I will call the method in it from controller ?
How to include the model in controller?
Laravel 5.3
Save them in app/models folder and you can access it by constucting it,Make sure you have ran composer dump-autolad
public function __construct(YourModel $model) {
parent::__construct();
$this->model = $model;
}
in your code simply model->test();it should work.
You create the folder Modelswherever you want and add your files as you wish, just make sure to properly namespace it. As long as Composer can autoload the class.
You can manually create app/Models/User.php and just make sure to namespace it:
namespace App\Models;
class User {
// your code
}
And to call the model method from a controller use the namespace of the model:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\YourModel;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class YourModelController extends Controller
{
public function whatever(){
$yourmodel->method();
}
}
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
{
I have create Helpers folder inside app, then I have created php file amchelpers.php ---> app/Helpers/amchelpers.php
amchelpers.php code:
<?php namespace App;
class AmcHelper {
static function displayString($string){
return $string;
}
}
then added these lines to composer.json
"files": [
"app/Helpers/amchelpers.php"
]
then run this command:
composer dump-autoload
then added 'Helper' => app_path() . '\Helpers\AmcHelper' to aliases array in config/app.php file.
in my controller I have below action (this action defined in route.php):
use Helper;
class UserController extends Controller {
public function displayMyString(){
echo Helper::displayString('Hello');
}
}
when run the page http://localhost:8080/easy_marketing/public/displayMyString
I Got:
ErrorException in compiled.php line 6367: Class 'C:\wamp\www\easy_marketing\app\Helpers\AmcHelper' not found
you have written user Helper instead of use Helper
or
another way to achieve this is
Laravel 5 App directory is autoloaded by default with its folder, what you have to take care is add namespace followed by directory name,
so directory structure is App --> Helpers
so your name space must include App\Helpers
try following code
<?php namespace App\Helpers;
class AmcHelper {
static function displayString($string){
return $string;
}
}
and when you are using this class in another class write this after namespace declaration
use App\Helpers\AmcHelper as Helper;
class UserController extends Controller {
public function displayMyString(){
echo Helper::displayString('Hello');
}
}