Loading a Parent Class in Symfony 2 - php

I've set up a test parent class in my Symfony 2 controller as follows:
<?php
namespace Zetcho\AmColAnBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class BaseController extends Controller
{
public function validateUser()
{
$user['first_name'] = "Name";
$user['signin'] = true;
return $user;
}
}
class DefaultController extends BaseController
{
public function indexAction()
{
$user = $this->validateUser();
$displayParms['user'] = $user;
return $this->render('ZetchoAmColAnBundle:Default:index.html.twig',$displayParms);
}
}
The code is in src/Zetcho/AmColAnBundle/Controller/DefaultController.php
The test code works. I'd now like to move the parent class (BaseController) out of the controller file to its own so I can reuse it in my other controllers. I want to put it in the same directory as the other controllers and I'd like to declare it the same way as the Controller in the use statement above. What's the best/accepted way to do this in Symfony 2?

You do this in Symfony2 exactly the same way as you would with any PHP class. Split your classes into separate files like this:-
src/Zetcho/AmColAnBundle/Controller/BaseController.php
namespace Zetcho\AmColAnBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class BaseController extends Controller
{
public function validateUser()
{
$user['first_name'] = "Name";
$user['signin'] = true;
return $user;
}
}
src/Zetcho/AmColAnBundle/Controller/DefaultController.php
namespace Zetcho\AmColAnBundle\Controller;
use Zetcho\AmColAnBundle\Controller\BaseController;
class DefaultController extends BaseController
{
public function indexAction()
{
$user = $this->validateUser();
$displayParms['user'] = $user;
return $this->render('ZetchoAmColAnBundle:Default:index.html.twig',$displayParms);
}
}
Its really quite simple once you know how. Remember that controllers in symfony2 are just normal PHP classes, there is nothing special about them.

Related

Laravel external class files

I have a file that i put in app\Classes\myVendor\dev_client_api.php. This file has a class in it:
class someClass{
//stuff
}
I want to use this class in a controller.
In my controller I have done the following:
namespace App\Classes\myVendor;
use dev_client_api;
class myController extends Controller
{
///stuff
public function processData(Request $request){
$client = new someClass($vars);
}
}
When i execute this page I get:
Class 'App\Classes\myVendor\Controller' not found
I have to admit I am not sure what exactly I am doing. Any help would be great.
I assume your Controllers are in Laravel's default App\Http\Controller directory.
namespace App\Classes\myVendor;
class someClass {
//stuff
}
namespace App\Http\Controllers;
use App\Classes\myVendor\someClass;
class myController extends Controller
{
///stuff
public function processData(Request $request){
$client = new someClass($vars);
}
}

Laravel, namespaces and PSR-4

I'm trying to set up PSR-4 within a new Laravel 4 application, but I'm getting some troubles achieving what I want when it comes to build controllers.
Here's what I have now :
namespace MyApp\Controllers\Domain;
class DomainController extends \BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = \View::make('domain.home');
}
}
I'm not so fond of using \View, \Config, \Whatever to use Laravel's classes. So I was wondering if I could put a use Illuminate\View; to be able to use View::make without putting a \.
Unfortunately, while doing this, I'm getting the following error : Class 'Illuminate\View' not found.
Could somebody help with this please ?
The problem in your case is that View is not located in Illuminate namespace but in Illuminate\View namespace, so correct import would be not:
use Illuminate\View;
but
use Illuminate\View\View;
You can look at http://laravel.com/api/4.2/ to find out which namespace is correct for class you want to use
Assuming BaseController.php has a namespace of MyApp\Controllers\Domain
namespace MyApp\Controllers\Domain;
use View;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home');
}
}
If BaseController.php has other namespace, i.e MyApp\Controllers
namespace MyApp\Controllers\Domain;
use MyApp\Controllers\BaseController;
use View;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home');
}
}
If, for instance, you controller needs to use another base class from Laravel, lets say Config.
namespace MyApp\Controllers\Domain;
use MyApp\Controllers\BaseController;
use View;
use Config;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home')->withName(Config::get('site.name'));
}
}
The use of View::make() takes advantage of the Laravel facades. To properly reference the facade, instead of directly referencing the class that gets resolved out of the iOC container, I would use the following:
use Illuminate\Support\Facades\View;
This will reference the View facade that is being used when calling View::make()

Yii controller inheritance ( in submodule )

My question is, how can I inherit Controllers action in YII, like:
class MainController extends FController
{
public function ActionIndex()
{
$this->render("index"); //the view;
}
}
--------------------------------------------
class SecondController extends FController
{
public function ActiondIndex()
{
MainController::ActionIndex();
}
}
Actually in my case the SecondController is the DefaultController of a sub-module. I want to make single code based webpage.
Since PHP does not support multiple inheritance, you may access the base class via the parent keyword.
class MainController extends FController
{
public function ActionIndex()
{
$this->render("index"); //the view;
}
}
class SecondController extends FController
{
public function ActionIndex() // Note: you mistyped the name of this action in your example
{
parent::ActionIndex();
}
}
Although you cannot reach the grandparent's ActionIndex method directly, you have to create a workaround for that.

Laravel: Load method in another controller without changing the url

I have this route: Route::controller('/', 'PearsController'); Is it possible in Laravel to get the PearsController to load a method from another controller so the URL doesn't change?
For example:
// route:
Route::controller('/', 'PearsController');
// controllers
class PearsController extends BaseController {
public function getAbc() {
// How do I load ApplesController#getSomething so I can split up
// my methods without changing the url? (retains domain.com/abc)
}
}
class ApplesController extends BaseController {
public function getSomething() {
echo 'It works!'
}
}
You can use (L3 only)
Controller::call('ApplesController#getSomething');
In L4 you can use
$request = Request::create('/apples', 'GET', array());
return Route::dispatch($request)->getContent();
In this case, you have to define a route for ApplesController, something like this
Route::get('/apples', 'ApplesController#getSomething'); // in routes.php
In the array() you can pass arguments if required.
( by neto in Call a controller in Laravel 4 )
Use IoC...
App::make($controller)->{$action}();
Eg:
App::make('HomeController')->getIndex();
and you may also give params
App::make('HomeController')->getIndex($params);
You should not. In MVC, controllers should not 'talk' to each other, if they have to share 'data' they should do it using a model, wich is the type of class responsible for data sharing in your app. Look:
// route:
Route::controller('/', 'PearsController');
// controllers
class PearsController extends BaseController {
public function getAbc()
{
$something = new MySomethingModel;
$this->commonFunction();
echo $something->getSomething();
}
}
class ApplesController extends BaseController {
public function showSomething()
{
$something = new MySomethingModel;
$this->commonFunction();
echo $something->getSomething();
}
}
class MySomethingModel {
public function getSomething()
{
return 'It works!';
}
}
EDIT
What you can do instead is to use BaseController to create common functions to be shared by all your controllers. Take a look at commonFunction in BaseController and how it's used in the two controllers.
abstract class BaseController extends Controller {
public function commonFunction()
{
// will do common things
}
}
class PearsController extends BaseController {
public function getAbc()
{
return $this->commonFunction();
}
}
class ApplesController extends BaseController {
public function showSomething()
{
return $this->commonFunction();
}
}
if you were in AbcdController and trying to access method public function test() which exists in OtherController you could just do:
$getTests = (new OtherController)->test();
This should work in L5.1

How to load an Entity from another Bundle on Symfony2

Let's say I have two Bundles :
Compagny\InterfaceBundle
Compagny\UserBundle
How can I load an Entity of UserBundle in the controller of InterfaceBundle ?
The Controller of my Compagny/InterfaceBundle :
<?php
// src/Compagny/InterfaceBundle/Controller/DefaultController.php
namespace Compagny\InterfaceBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Compagny\UserBundle\Entity; // I believed this line will do the trick, but it doesn't
class DefaultController extends Controller
{
public function indexAction()
{
$user = new User();
}
}
The Entity of my Compagny/UserBundle :
<?php
namespace Compagny\UserBundle\Entity
class User {
public $name;
public function setName($name) {
// ...
}
public function getName() {
// ...
}
}
(Let's says for this example that the User class doesn't use Doctrine2, because it doesn't need to connect to the database).
<?php
// src/Compagny/InterfaceBundle/Controller/DefaultController.php
namespace Compagny\InterfaceBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Compagny\UserBundle\Entity\User; // It's not a trick, it's PHP 5.3 namespacing!
class DefaultController extends Controller
{
public function indexAction()
{
$user = new User();
}
}
You can of course, you are just using a class from another namespace. The fact that it is an entity is not important at all! You can of course query the entity manager for that entity as well.

Categories