Laravel 5 class inheritance - php

I am working with laravel 5 code.
I cannot seem to extend class that has different namespace than child class.
My parent class is
The folder is App/Http/Controllers/
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
class MainController extends BaseController{
function __construct() {
}
function home() {
echo "main";
}
}
And my child class is
The folder is App/Http/Controllers/Site
namespace App\Http\Controllers\Site;
class SiteController extends MainController {
function __construct() {
parent::__construct();
}
function home() {
return view('dashboard');
}
}
And my routes file has this route
Route::get('/', 'Site\SiteController#home');
This is the error that I get
FatalErrorException in SiteController.php line 5: Class 'App\Http\Controllers\Site\MainController' not found
This clearly is the issue with namespace because when I do
Route::get('/', 'Site\SiteController#home');
It echos the method home in MainController
How do I get this to work?

This is not Laravel 5 problem but the common PHP namespace problem.
There are two ways to achieve the result:
use use statement: add this line use \App\Http\Controllers\MainController;
use full namespace: change your line to class SiteController extends \App\Http\Controllers\MainController
Choose the way you like more.

Related

Target class [App\Http\Controllers\Admin\Category\CategoryController] does not exist

I am getting really confused on how namespace and use are being used here to reference my CategoryController. I am using laravel 8. Any guidance would be greatly appreciated!
My route uses resource and that seems to be ok.
web.php:
namespace App\Http\Controllers\Admin\Category;
Route::get('/admin/categories', [CategoryController::class, 'category'])->name('categories');
My Category Controller is App\Http\Controllers\Admin\Category\CategoryController.php as shown here is my folder structure:
CategoryController.php:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
public function __construct()
{
$this->middleware('auth:admin');
}
public function category(){
echo "Hello Category";
}
}
Your namespaces should mimic your directory structure. So if you have a CategoryController.php file that resides in the folder app/Http/Controllers/Admin/Category, then the namespace for that file would be namespace App\Http\Controllers\Admin\Category;
namespace App\Http\Controllers\Admin\Category;
use App\Http\Controllers\Controller;
class CategoryController extends Controller
{
// stuff
}
When referencing classes, you include the relevant namespace with the use statement.
So in your web.php file you want to replace:
namespace App\Http\Controllers\Admin\Category;
with:
use App\Http\Controllers\Admin\Category\CategoryController;

Class Controller not found in laravel 5

So I am new to laravel and was just trying out some code to clear the basics,but however after creating a new controller to handle a route.It throws a fatal exception saying Class 'Controller' not found!
(The controller.php exists in the same directory)
The controller.php code is the default one
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
abstract class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
This is my PagesController.php code
<?php
class PagesController extends Controller
{
public function home()
{
$name = 'Yeah!';
return View::make('welcome')->with('name',$name);
}
}
This is route.php code
<?php
Route::get('/','PagesController#home');
The welcome.blade.php code is the default one except it displays the variable $name instead of laravel 5.
What am I getting wrong?
When you reference a class like extends Controller PHP searches for the class in your current namespace.
In this case that's a global namespace. However the Controller class obviously doesn't exists in global namespace but rather in App\Http\Controllers.
You need to specify the namespace at the top of PagesController.php:
namespace App\Http\Controllers;
You will want to specify the namespace to your Controller class:
class PagesController extends \App\Http\Controllers\Controller
otherwise Controller is looked up in the default root namespace \, where it does not exist.
My issue was a different class name than the one in the class that extends controller, the names should be same
If you are creating a controller inside a subfolder with a different namespace then use this on your controller file:
use App\Http\Controllers\Controller;

Laravel Class BaseController not found

I'm newbie in laravel. So I've created TestController.php file, inside file:
<?php
class TestController extends BaseController {
public function testFunction()
{
return "It works";
}
}
In routes:
Route::get('test', 'TestController#testFunction');
I'm got this error:
FatalErrorException in TestController.php line 2:
Class 'BaseController' not found
I'm using laravel 5, I'm trying to change from BaseController to Controller, not works :( What's wrong here? Thanks in advance :(
P.S I'm tried change to "\BaseController". Not works.
There is no such thing as a BaseController in Laravel, use Controller instead. BaseController is used once throughout the framework, in the Controller class, but only as a use as alias:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController; // <<< See here - no real class, only an alias
use Illuminate\Foundation\Validation\ValidatesRequests;
abstract class Controller extends BaseController
{
use DispatchesJobs, ValidatesRequests;
}
Your controller needs this:
<?php namespace App\Http\Controllers;
class TestController extends Controller
{
public function testFunction()
{
return "It works";
}
}
Is your TestController in app/Http/Controllers?

How do I call a model in Laravel 5?

I'm trying to get the hang of Laravel 5 and have a question which is probably very simple to solve but I'm struggling.
I have a controller named TestController and it resides in \app\Http\Controllers
This is the controller:
<?php
namespace App\Http\Controllers;
class TestController extends Controller {
public function test()
{
$diamonds = diamonds::find(1);
var_dump($diamonds);
}
}
Then I have a model I created that resides in /app:
<?php
namespace App;
class diamonds extends Model {
}
Put all other mistakes aside which I'm sure there are, my problem is that laravel throws an error:
FatalErrorException in TestController.php line 10: Class
'App\Http\Controllers\diamonds' not found
So, how do I get the controller to understand I'm pointing to a model and not to a controller?
Thanks in advance...
You have to import your model in the Controller by using namespaces.
E.g.
use App\Customer;
class DashboardController extends Controller {
public function index() {
$customers = Customer::all();
return view('my/customer/template')->with('customers', $customers);
}
}
In your case, you could use the model directly App\diamonds::find(1); or import it first with use App\diamonds; and use it like you already did.
Further, it is recommended to use UpperCamelCase class names. So Diamonds instead of diamonds. You also could use dd() (dump and die) instead of var_dump to see a nicely formatted dump of your variable.
//Your model file
<?php
namespace App\Models;
class diamonds extends Model {
}
// And in your controller
<?php
namespace App\Http\Controllers;
use App\Models\
class TestController extends Controller {
public function test()
{
$diamonds = diamonds::find(1);
var_dump($diamonds);
}
}
Try adding the following lines above your class decleration in your controller file...
use App\Diamonds;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
(this assumes your model file is called Diamonds.php)
Ultimately, there were a few issues here and all of you helped, however I was missing the following on the model page:
use Illuminate\Database\Eloquent\Model;

Zend Framework 2 model class not found

I have a model in src\Front\Model\FrontModel.php
I am trying to extend it in my IndexController i have this in my Module.php:
use Front\Model\FrontModel;
But i always get this error:
Fatal error: Class 'Front\Model\FrontModel' not found in
C:\Apache24\htdocs\cartbiz\module\Front\src\Front\Controller\IndexController.php
on line 16
I have this in my IndexController where i am trying to extend my model my Controller resides in src\Front\Controller\IndexController.php
namespace Front\Controller;
use Front\Model\FrontModel;
class IndexController extends FrontModel
{
/* Initialize Controller */
public function initAction()
{
parent::initAction();
}
}
I have this as my model class my model class resides in src\Front\Model\FrontModel.php
namespace Front\Model\FrontModel;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class FrontModel extends AbstractActionController
{
/* Application initializer
** All front application logic
*/
public function __construct ()
{
die('ssss');
$this->_viewManager=new ViewModel;
$this->_viewManager->setTemplate('front/index/index');
return $this->_viewManager;
}
}
Any help is appreciated
You need to add a namespace to the FontModel class.
namespace Front\Model;
use Zend\Mvc\Controller\AbstractActionController;
class FrontModel extends AbstractActionController
{}
Also, it's worth noting that your naming conventions could lead to confusion. I would recommend placing all the controllers in the controller folder, and reading up on the coding standards.
Tested and works
namespace Front\Model;
use Zend\Mvc\Controller\AbstractActionController;
class FrontModel extends AbstractActionController
{
/* Application initializer
** All front application logic
*/
public function __construct ()
{
die('ssss');
$this->_viewManager=new ViewModel;
$this->_viewManager->setTemplate('front/index/index');
return $this->_viewManager;
}
}

Categories