Yii Framework : call Controller's function from CAction class - php

I want to make a modular Controller by separating Actions in different class.
The problem is some of the actions are calling a private function of the controler.
Controller :
class ApiController extends Controller
{
public function actions()
{
return array(
'index'=>'application.controllers.api.IndexAction',
);
}
..........
private function _sendResponse($status = 200, $body = '', $content_type = 'text/html')
{
// some code
}
}
In IndexAction.php I tried like this, but doesn't work :
class IndexAction extends CAction
{
public function run()
{
$this->getController()->_sendResponse(204); //this error
}
}
the exception is
ApiController and its behaviors do not have a method or closure named "_sendResponse".
Is this possible what i'm trying to do?
am I missing something here?

I looks like you try to access a private Method out of the Scope of your Class. Not even Classes that inherit can access a private Method.
Try public function _sendResponse()

Related

Laravel Reusing Controller Logic

I have multiple controllers, with multiple methods, which all return views.
class PageController extends Controller {
public function index()
{
// do lots of stuff
return view('view.name', $lotsOfStuffArray);
}
public function list()
{
//...and so on
}
I now have the need to create an API, which performs much of the same logic as the methods above, but returns a JSON output instead:
class PageApiController extends Controller {
public function index()
{
// do lots of the same stuff
return $lotsOfStuffCollection;
}
public function list()
{
//...and so on
}
What is the best way to accomplish this without having to copy and paste code from one controller to the other?
I've tried placing a lot of the logic into traits and using them in my Eloquent models, but that still requires that I copy and paste code from controller to controller. I should also note its not viable to check expectsJson() and return a response accordingly as I have many, many methods.
Is it a good idea to have the logic stored in a parent class and then create a child controller that responds with a view and a child controller that responds with JSON?
You could abstract the logic to a service class. I have answered a similar question.
You have PageController, PageAPIController and PageService.
class PageService {
public function doStuff()
{
return $stuff;
}
}
class PageController extends Controller {
public function index()
{
$service = new PageService();
$stuff = $service->doStuff();
return $stuff;
}
}
class PageAPIController extends Controller {
public function index()
{
$service = new PageService();
$stuff = $service->doStuff();
return $stuff->toJSON();
}
protected function toJSON(){
//You could also abstract that to a service or a trait.
}
}

phalcon controller indexAction break down

I am new into Phalcon framework. I just got the basic idea about it. Every controller has methods with multiple specific actions. I wrote a huge indexAction method but now I want to break it down with multiple private method so that I can reuse those functionality. But when I try to create any method without action suffix, it returns error(Page Not Found). How can I break it down into multiple methods?
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
$this->someMethod();
}
public function someMethod()
{
//do your things
}
}
Controllers must have the suffix “Controller” while actions the suffix “Action”. A sample of a controller is as follows:
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
}
public function showAction($year, $postTitle)
{
}
}
For calling another method, you would use it straight forward
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
echo $this->showAction();
}
private function showAction()
{
return "show";
}
}
Docs.
What exactly do you want? The answer seems trivial to me.
class YourController extends Phalcon\Mvc\Controller
{
// this method can be called externally because it has the "Action" suffix
public function indexAction()
{
$this->customStuff('value');
$this->more();
}
// this method is only used inside this controller
private function customStuff($parameter)
{
}
private function more()
{
}
}

PHP - How to extend and access parent constructor properties

I am attempting to access the parent class __construct properties within a child class that extends this, however not sure how to do this as I have tried multiple methods and didn't give me the expected result.
So I have a baseController and a indexController which extends it, I want to be able to have direct access to the properties of the parent within the child controller.
$config = ['site' => 'test.com'];
class baseController {
public function __construct($config){
$this->config = $config;
}
}
class indexController extends baseController {
public function __construct(){
parent::__construct(); // doesnt seem to give any outcome
}
public static function index() {
var_dump($this->config); // need to access within this method
}
}
$app->route('/',array('indexController','index')); // the route / would call this controller and method to return a response
There are several issues with code you have there. You are setting up config as a global, it should be inside your BaseController and set it to public or protected:
class BaseController {
protected $config = ...
Just like #mhvvzmak1 mentioned, your child constructor is calling the parent properly. for example you can do it like so:
class IndexController extends BaseController {
public function __construct(){
$config = [];
parent::__construct($config);
}
and finally just like dan08 mentioned, you can't reference $this from a static method, change your index function:
public function index() {
Update
If you really want the child function to remain static as required by your framework, you make config a static function on the BaseController and call it in the child.
class BaseController {
protected static function config() {
return ['site' => 'mySite'];
}
}
class Child extends BaseController {
public static function index() {
$config = BaseController::config();
}
}

How would I pass in the request class to my abstract class?

I'm developping an API and because I have (mostly) the same functionality I've created an abstract class to be extended on my controllers.
My abstract class looks like: http://laravel.io/bin/23Bzj
Where in the controller I would construct with a model and response (will probably move the response to ApiController constructor later).
class EventController extends ApiController
{
public function __construct(Event $model, ResponseRepository $response)
{
$this->model = $model;
$this->response = $response;
}
}
But the question is: how will I be able to use the specific Request class in my ApiController to be used the in the methods for validation/what is the best practice.
I can use a normal Request class but then I won't have any validation before the methods.
When I'm in my EventController I will be able to use UpdateEventRequest and CreateEventRequest and so on.
As far as I know if you use in your controller in any method
public function edit(UpdateEventRequest $req) {
// any code
}
before launching // any code part validation will be done.
What you could try to do:
Change your update method in abstract class to protected
Change signature of this method from public function update(Request $request, $id) to public function update($request, $id) - I don't know it this step will be necessary
Create new method for example realUpdate with the following code:
public function realUpdate(UpdateEventRequest $req, $id) {
parent::update($req, $id);
}
I'm not sure about step 2 because I don't know if Laravel will try to run any validation if you use Request in your abstract class. It's also possible that it will run this validation again for UpdateEventRequest - you should give a try, I haven't tested it.
Basically you will have code similar to this:
<?php
class X
{
}
class Y extends X
{
}
abstract class ApiController
{
protected function update(X $x, $id)
{
echo "I have " . get_class($x) . ' and id ' . $id;
}
}
class Controller extends ApiController
{
public function realUpdate(Y $y, $id)
{
parent::update($y, $id);
}
}
$c = new Controller();
$c->realUpdate(new Y, 2);
and Laravel should run at least once validator based on rules from UpdateEventRequest.
You cannot have the same name for this method in child class because you will get a warning:
Strict Standards: Declaration of Controller::update() should be
compatible with ApiController::update(X $x, $id) in ... line 31
It will however still work but I assume you don't want to have any warnings.

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

Categories