Normal function in Extbase controller - typo3

Is it possible to write a normal function in the controller?
I want to clean up my code a bit, so I want to write some methods for repeated code segments, but I don't want to create a special class.
How is it possible to do this?
If I do a normal
private function xyz () {}
I got a function not found error.

You should use protected, not private unless you have very good reasons to do so. Anyway, defining additional methods work fine for me.
You need to call this method with $this->xyz().

A good solution might be using an abstract class if you want to share methods accross controllers:
abstract class AbstractController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController{
protected function myFunction(){}
}
Your controllers inherit from the abstract class and will have all methods available:
class FirstController extends AbstractController {
public function firstAction(){
// has access to myFunction()
}
}
class SecondController extends AbstractController {
public function secondAction(){
// has access to myFunction()
}
}

Related

call a Method from One Repository to another Repository Symfony2

Hi I am using symfony2 With ODM, I want to call a function from one reposotory to another repository to re-use it. I did not get a way to call it directly.
Following my code.
//My LedgerRepository.php
class LedgerRepository extends DocumentRepository
{
public function ProfitLoss(){
//Some re-usable code
}
}
//My BudgetRepository.php
class BudgetRepository extends DocumentRepository
{
//So here I want to call method ProfitLoss() from LedgerRepository
}
how to make it possible please guide.
Thanks advance
In this case good old inheritance may come to the rescue. Both Ledger and Budget deal with financial transactions. Why not this?:
class TransactionsRepository extends DocumentRepository
{
public function ProfitLoss() {}
}
class LedgerRepository extends TransactionsRepository {}
class BudgetRepository extends TransactionsRepository {}
In this case both Ledger and Budget can "share" methods in TransactionsRepository.

AS3 Eclipse: How to create template to extends myClass?

How do I create a template that each time when I create a class that extends MyClass, it will automatically add 3 functions.
EDIT:
In other words I am trying to implement Abstract functionality in AS3. Assume that MyClass have both private and protected methods.
I see the only way to write own code template and call it every time you need, in Flash Builder: window->preference->flash builder->editors->code template->action script->new and give the name to the template, for instance myclass.
You can use existed templates as an example for template syntax.
Template code for MyClass child class with three methods:
import my.package.MyClass
/**
* #author ${user}
*/
public class ${enclosing_type} extends MyClass
{
public function ${enclosing_type}()
{
}
override public function publicMethod():void
{
}
override protected function protectedMethod():void
{
}
override private function privateMethod():void
{
}
${cursor}
}
Usage:
Create new "action script file" or "new class",
remove all file content
type myclass and choose from auto-complete options template myclass
If you are actually extending MyClass, all of MyClass's functions are already available to your descendants. You can also override either of them with old header and desired new body, and still be able to call older versions of those functions via super qualifier. So, you add those functions to MyClass and let them be.
Another way is to make an interface - it's a set of declarations without any function bodies, which you have to implement in any class that wants this interface in its content. A short introduction to interfaces. Then your MyClass will be an interface, with 3 function declarations in it, and whichever class will be declared as implements MyClass will have to provide bodies for these functions.
Check other keywords on that page, including extends and implements.
Hope this helps.
EDIT: There are no abstract classes in AS3, however you can emulate abstract functions in a normal class via exception throwing:
protected function abstractFunction(...params):void {
throw new Error("Abstract!");
}

Wrapping JavaScriptObjects in case of multiple subclasses?

I have a bunch of data entities that all implement Entity. Now I want to expose some of these entities to JavaScript code, but I can't just make a bunch of JavaScriptObject subclasses because of the one-implementation rule.
So, I'm using this kind of thing:
public class JsStandardScale3 implements StandardScale3 {
private JavaScriptObject wrapped;
public JsStandardScale3(JavaScriptObject wrapped) {
this.wrapped = wrapped;
}
#Override
public native Long getLicenseId() /*-{
this.#com.activegrade.client.exported.JsStandardScale3::wrapped.getLicenseId();
}-*/;
This works, it's just a lot of work. The overlay type structure is so much nicer. Any suggestions?
It turns out that you CAN extend JavaScriptObject with multiple subclasses of an interface as long as all of your extensions are from a single "root" extension of JSO.
For example, I have the structure Standard extends Entity and Course extends Entity. I could NOT do:
JsStandard extends JavaScriptObject...
JsCourse extends JavaScriptObject...
but I could do:
JsEntity extends JavaScriptObject...
JsStandard extends JsEntity...
JsCourse extends JsEntity...
fantastic!
The only limitation is that every method must be marked final, which works fine for a simple overlay scenario.

Zend Framework - How can i share common code between some of a controller's actions?

To keep my controllers as DRY as possible i need to share some common code (a big chunk of code) between say 2 of my controller's actions and not all of them and i need access variables in this shared code in my actions.
For example:
class FirstController extends Zend_Controller_Action {
public function firstAction() {
//common code here: contains an array $columns
}
public function secondAction() {
//common code here: contains an array $columns also
}
//other actions
}
so how can I refactor this to put the common code in one place and be able to access $columns and in firstAction() and secondAction().
Thanks.
I don't recommend you to use a base controller. It's overkilling and heavy for such a small task. Since you want to share common code within one controller, use instead an action helper and a class attribute $columns that you can send as argument to your action helper.
Read more about action helpers here.
Action Helpers allow developers to inject runtime and/or on-demand
functionality into any Action Controllers that extend
Zend_Controller_Action. Action Helpers aim to minimize the necessity
to extend the abstract Action Controller in order to inject common
Action Controller functionality.
You can create new class and extend Zend_Controller_Action then extend your newly created class not Zend_Controller_Action
example:
class CommonactionsController extends Zend_Controller_Action {
public function firstAction() {
//common code here : contains an array $columns
}
public function secondAction() {
//common code here : contains an array $columns also
}
//other actions
}
and then:
class FirstController extends CommonactionsController {
// here you can use all your common actions...
}
second controller..
class SecondController extends CommonactionsController {
// here you can use all your common actions...
}
and so on...

How to automatically set and attribute to controller

Maybe the question is not self-explanatory, so I will explain it through.
The deal is: I got the variable $conn in the bootstrap class file. I'd like to make it global for every controller so that I just have to call $this->conn in the controller action scope in order to access the data inside. How would I do it?
Thx
One fairly straightforward way is to create your own base class form which your controller's inherit:
<?PHP
class My_Controller_Action extends Zend_Controller_Action {
public $conn;
public function init(){
//set $this->conn
}
}
class Some_Real_Controller extends My_Controller_Action {
//$this->conn exists!
}
class Some_Other_Real_Controller extends My_Controller_Action {
//$this->conn exists here too!
}
Matthew Weier O'Phinney posted a blog entry recently with some examples of how to use action helpers to do this, see:
http://weierophinney.net/matthew/archives/235-A-Simple-Resource-Injector-for-ZF-Action-Controllers.html
this will achieve the same thing without having to use a base controller class.