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;
}
}
Related
I am creating a webapp that has some common functions. So I figured the easiest way to do this would be to make a base controller and just extend that. So in the base controller I have (similar to):
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class BaseController extends Controller
{
protected function dosomething($data)
{
return $data;
}
}
And then in the default controller:
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends BaseController
{
/**
* #Route("/", name="homepage")
*/
public function indexAction()
{
$data = "OK";
$thedata = $this->dosomething($data);
}
}
And then for the Admin Controller:
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class AdminController extends BaseController
{
/**
* #Route("/", name="homepage")
*/
public function indexAction()
{
$data = "OK";
$thedata = $this->dosomething($data);
}
}
However, I am getting errors like "Compile Error: Access level to AppBundle\Controller\AdminController::dosomething() must be protected (as in class AppBundle\Controller\BaseController) or weaker", not just when I load the admin controller function, but default as well. When I stop the admin controller extending base controller, this error goes (seems to work on default but not admin).
I'm guessing somewhere I have to let Symfony know that the admin controller is safe or something?
It has nothing to do with Symfony, it's PHP.
Obviously, you're trying to redefine dosomething method in your Admin Controller, and trying to make this method private.
It's not allowed. It may be either protected or public.
It's principle of OOP. Because if you would have a class SubAdminController, then instance of it would be also instance of both AdminController and BaseController. And PHP must definitely know if the method dosomething from parent class is accessible from SubAdminController.
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?
I have the following scenario:
Controller:
class Collect extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('phirehose');
$this->load->library('oauthphirehose');
$this->load->library('ghettoqueuecollector');
}
function index() {
// Start streaming/collecting
$this->ghettoqueuecollector('datos');
...
}
Class:
A)
class Ghettoqueuecollector extends Oauthphirehose {
...
}
B)
abstract class Oauthphirehose extends Phirehose {
...
}
C)
abstract class Phirehose {
...
}
When I try to use the controller gives this error:
"Fatal error: Cannot instantiate abstract class Phirehose"
That is lacking adapt? Codeigniter outside these classes work using require. Can I use them in CI? Thanks
$this->load->library() is made to include, instantiate and configure classes, it's not just an alias for require() - you can't use it in that manner.
If you need to extend an abstract class, you'll have to manually include/require it from the file that extends it, like this:
libraries/Oauthphirehose.php:
require_once __DIR__.'/Phirehose.php';
abstract class Oauthphirehose extends Phirehose {}
libraries/Ghettoqueuecollector.php:
require_once __DIR__.'/Oauthphirehose.php';
class Ghettoqueuecollector extends Oauthphirehose {}
Then you just load the one library that you actually need to instantiate:
$this->load->library('ghettoqueuecollector');
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.
I'm hoping somebody out there can help me. I am using laravel 4 and I'm writing my first unit tests for a while but am running into trouble. I'm trying to extend the TestCase class but I'm getting the following error:
PHP Fatal error: Class registrationTest contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Illuminate\Foundation\Testing\TestCase::createApplication) in /home/john/www/projects/MyPainChart.com/app/tests/registrationTest.php on line 4
Now if I have this right then the error is referring to the fact that is a method is abstract then the class it's in must also be abstract. As you can see from below the TestCase class it is abstract. I have searched for this error but have drawn a blank.
Trying to follow this cast on Laracasts https://laracasts.com/lessons/tdd-by-example and although you have to be a subscriber to watch the video the file is underneath it and as you can see I am doing nothing different to Jeffrey Way.
My Test:
<?php
class registrationTests extends \Illuminate\Foundation\Testing\TestCase
{
/**
* Make sure the registration page loads
* #test
*/
public function make_sure_the_registration_page_loads_ok()
{
$this->assertTrue(true);
}
}
The beginning of the TestCase class:
<?php namespace Illuminate\Foundation\Testing;
use Illuminate\View\View;
use Illuminate\Auth\UserInterface;
abstract class TestCase extends \PHPUnit_Framework_TestCase {
By the way - the Laravel testing class is not autoloaded by default and so I have tried both the fully qualified class name and use Illuminate\Foundation\Testing and then just extending TestCase. I know it can see it aswhen I don't fully qualify the name it complains that the class cannot be found. I've also tried:
composer dump-autoload
and
composer update
Any help appreciated
According to your error message: Class registrationTest contains 1 abstract method the Base Class contains an abstract method and when a Child Class extends another class with abstract methods then the child class should implement the abstract methods available in Base class. So, registrationTest is child class and \Illuminate\Foundation\Testing\TestCase is the base class and it contains an abstract method:
An abstract method in \Illuminate\Foundation\Testing\TestCase:
abstract public function createApplication();
So, in your child class/registrationTest you must implement this method:
public function createApplication(){
//...
}
But, actually you don't need to directly extend the \Illuminate\Foundation\Testing\TestCase because in app/tests folder there is a class TestCase/TestCase.php and you can extend this class instead:
// In app/tests folder
class registrationTest extends TestCase {
//...
}
The TestCase.php class looks like this:
class TestCase extends Illuminate\Foundation\Testing\TestCase {
public function createApplication()
{
$unitTesting = true;
$testEnvironment = 'testing';
return require __DIR__.'/../../bootstrap/start.php';
}
}
Notice that, TestCase has implemented that abstract method createApplication so you don't need to extend it in your class. You should use TestCase class as base class to create test cases. So, create your tests in app/tests folder and create classes like:
class registrationTest extends TestCase {
public function testBasicExample()
{
$crawler = $this->client->request('GET', '/');
$this->assertTrue($this->client->getResponse()->isOk());
}
}
Read Class Abstraction.
Firstly go in composer.json and add
"scripts" : {"test" : "vendor/bin/phpunit"
}
Then run composer update
Then
The TestCase.php class in path /test/ should look like
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Facades\Artisan;
abstract class TestCase extends BaseTestCase {
use CreatesApplication;
}
Then your registrationTests class should look like this
<?php
namespace Tests\Feature;
use Tests\TestCase;
class registrationTests extends TestCase {}
Just intake dependancy at the top of your class as follows and you are good to go,
<?php
namespace YOUR_CLASS_PATH;
use Tests\TestCase;
class UserTest extends TestCase{
...//your business logic here
}
I hope this works.