The default HomeController class is defined using
class HomeController extends BaseController {
However, when a resource controller is created via artisan, the class extends \BaseController instead of BaseController. Why is this, and what is the difference?
class TestResourceController extends \BaseController {
There is no difference (in a default installation). The \ simply tells PHP to use the root namespace instead of any other class with the same name but on a different namespace. If you were to create your own class called BaseController, PHP would not know which class to use unless it were explicity defined by the namespace, i.e. MyNamespace\BaseController.
Related
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;
I was trying to migrate a Laravel 4.2 app into Laravel 5.0
and previously in Laravel 4.2 you have a BaseController which other Controllers you create can extend, meaning if I add a method inside it. The other controllers will extending the BaseController can use it.
Now on Laravel 5.0, they somehow changed it instead of using class they made use of an abstract class
<?php namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
abstract class Controller extends BaseController {
use DispatchesCommands, ValidatesRequests;
}
Now I'm not familiar with what an abstract class is so my main concert is will I still be able to add functions() that can be used by other controllers extending it?
So as an example in a controller extending the BaseController
$this->method_from_base_controller();
Yes, you can create for abstract classes methods that will be used in classes that inherits from abstract class.
The main difference between abstract classes and normal classes is that you cannot create objects of abstract classes. You can also create in abstract classes methods you want to be implemented in child classes. Reference on abstract clasess on php.net
I have created a common class in app/Classes/Common.php
but whenever i try to access a model in a class function.
$new_booking_request = BookingRequest::where('host_id','=',Auth::id())
I am getting this error
Class 'App\Models\BookingRequest' not found
Even other classes like Auth, URL and Cookie are not working.
Is there a way to bring all classes in my Common class scope?
You get this issue when your namespace is wrong you or you forgot to namespace.
Since common.php is inside App/Classes, inside Common.php do somethng like this:
<?php namespace App\Classes;
use View, Auth, URL;
class Common {
//class methods
}
Also ensure your model class has the correct namespace, if BookingRequest.php is located inside App\Models then inside BookingRequest.php do this:
<?php namespace App\Models;
BookingRequest extends \Eloquent {
//other definitions
}
Then if you wish to use BookingRequest.php outside its namespace or in another namespace like so:
<?php namespace App\Classes;
use App\Models\BookingRequest;
use View, Auth, URL;
class Common {
//class methods
}
In Laravel 5 everything is namespaced, make sure your class has a proper namespace and that you are calling it using that same namespace you specified.
To include classes in another class make sure that you use the use keyword to import the necessary classes on top of your class definition. Also you can call the class globally with the \. Ex: \Auth, \URL and \Cookie
For the namespace in L5 here is a quick example:
<?php namespace App\Models;
class BookingRequest {
// class definition
}
then when trying to call that class, either call the full namespace path of the function, or include the function.
<?php
class HomeController extends Controller {
public function index()
{
$newBookingRequest = App\Models\BookingRequest::where('host_id','=',Auth::id());
}
}
OR
<?php namespace App\Controllers;
use App\Models\BookingRequest; // Include the class
class HomeController extends Controller {
public function index()
{
$newBookingRequest = BookingRequest::where('host_id','=',Auth::id());
}
}
PS:
Please use camelCase when defining class attributes and methods as this helps for a better code-styling and naming conventions when using the L5 framework.
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;
}
}
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.