Kohana framework. Kohana class question - kohana

I'm looking at Kohana framework and trying to go through the code to better understand how framework works.
So - from index.php we load:
require SYSPATH.'base'.EXT;
require SYSPATH.'classes/kohana/core'.EXT;
require APPPATH.'bootstrap'.EXT;
In core.php file we do the following:
public static $environment = Kohana::DEVELOPMENT;
What to we refer to by calling Kohana::DEVELOPMENT?
From what I understand - by using :: we should be getting static constant from kohana class. - right? But at that moment in the code there is no Kohana class loaded that I could find.
So - can someone explain what's going on here:) ?
Thanks
RESOLUTION:
never mind. I didn't follow the code far enough. Kohana class extends Kohana_Core class. mmm. too bad there is no way to delete dumb questions from StackOverflow.

Kohana (as probably any other framework) uses "auto loading" mechanism. This allows you to use classes without including the files they are defined in by hand. The autoloader will automatically include/require the file that the Kohana class is in.
So when you type Kohana::DEVELOPMENT or new Kohana (); the auto loader will load the file with the Kohana class in it. You should know that this does not work magically. You have to write your own auto loader code for your framework.
You can read more about auto loading here.

For further information check this link: spl_autoload register. Kohana uses own implementation, which can be set in the bootstrap.php file. You can found this funtion in the Core.php file.
/**
* Enable the Kohana auto-loader.
*
* #link http://kohanaframework.org/guide/using.autoloading
* #link http://www.php.net/manual/function.spl-autoload-register
*/
spl_autoload_register(array('Kohana', 'auto_load'));

Related

Kohana - Need advice about routing

application
modules
myModule
classes
controller
model
helper
foo.php
views
init.php
This is the file structure of my module. I need to create route inside of init.php, that will allow me to call foo.php file from anywhere in the project. The class in foo.php does not extend any kohana classes - that's the place stopping me.
Could you help me?
Routing applies to controllers, not random classes. As long as you register the module, the class will be autoloaded whenever you do new Foo;. They don't need to extend kohana classes to be autoloaded.
Try with: include Kohana::find_file('classes', 'foo'); and don't forget to enable your module in bootstrap.php: 'myModule' => MODPATH.'myModule'.

cakephp facebook component include paths on linux

I have a website on cakephp 1.3, with facebook integration. On my localhost(with windows os) it works fine, but on real hosting(with linux os) it gives this error Fatal error: Class 'Facebook' not found in /home/username/public_html/app/plugins/facebook/libs/f_b.php on line 22.
This is f_b.php
App::import('Vendor', 'Facebook.facebook/src/facebook');
App::import('Lib', 'Facebook.FacebookInfo');
class FB {
/**
* Facebook Api
*/
public static $Facebook = null;
public function __construct() {
if (empty(self::$Facebook)) {
self::$Facebook = new Facebook(FacebookInfo::getConfig()); } // 22 line
}
so, apparently it can not load Vender, I think there is a problem with paths, though this does not work either App::import('Vendor', 'Facebook.facebook'.DS.'src'.DS.'facebook');
f_b.php located in plugins/facebook/libs/ folder and this Facebook.php vendor is located in plugins/facebook/vendors/facebook/src folder
Thanks
Finally I found the solution of my problem, in order to work both on windows and linux it is necessary write like this
App::import('Vendor', 'Facebook.Facebook', array('file' => 'facebook/src/Facebook.php' ));
Here, are couple of example for that - Vendor examples
I’d say your paths don’t look right (why is there a dot?).
When I’ve used plugins with CakePHP, I’ve had to enable them by un-commenting the Plugins::loadAll() line in app/config/bootstrap.php. This will then make my library available in my CakePHP application.

Autoloading a class in Symfony 2.1

I'm porting a Symfony 1.2 project to Symfony 2.x. I'm currently running the latest 2.1.0-dev release.
From my old project I have a class called Tools which has some simple functions for things like munging arrays into strings and generating slugs from strings. I'd like to use this class in my new project but I'm unclear how to use this class outside of a bundle.
I've looked at various answers here which recommend changing app/autoload.php but my autoload.php looks different to the ones in the answers, maybe something has changed here between 2.0 and 2.1.
I'd like to keep my class in my src or app directories as they're under source control. My vendors directory isn't as I'm using composer to take care of that.
Any advice would be appreciated here.
Another way is to use the /app/config/autoload.php:
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__.'/../vendor/autoload.php';
$loader->add( 'YOURNAMESPACE', __DIR__.'/../vendor/YOURVENDOR/src' );
// intl
if (!function_exists('intl_get_error_code')) {
require_once _DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
$loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;
Just replace YOURNAMESPACE and YOURVENDOR with your values. Works quite well for me, so far.
You're correct, I stumbled upon the changes in autoload from 2.0 to 2.1. The above code works fine with the latest version, to which I upgraded my project ;-)
For a simple case like this the quickest solution is creating a folder (for example Common) directly under src and put your class in it.
src
-- Common
-- Tools.php
Tools.php contains your class with proper namespace, for example
<?php
namespace Common;
class Tools
{
public static function slugify($string)
{
// ...
}
}
Before calling your function do not forget the use statement
use Common\Tools;
// ...
Tools::slugify('my test string');
If you put your code under src following the proper folder structure and namespace as above, it will work without touching app/autoload.php.

Can somedy explain me the TAB SAMPLE (gwtp)?

I am using GWTP. I did the nested presenter tutorial. But there is no tutorial for the SAMPLE TAB application (the one with the admin tab appearing if you switch to the admin mode). Can somebody explain me the main concepts of this application ? Tkx.
Update: Update: Now you can download the workable sample Maven project from here: gwtp-sample-tab.zip
I used the tabbed presenter feature successfully in my project (I found the sample code didn't compile as well). I think the first thing is to make it work, and then learn it and feel the benefits gradually :)
Here is the steps I did:
1) Copy the following files
BaseTab.java
BaseTabPanel.java
SimpleTab.java
SimpleTabPanel.java
SimpleTab.ui.xml
SimpleTabPanel.ui.xml
UiModule.java
from the sample code to you project. For example, I copied to this package: com.widenhome.web.client.ui. Also please remember to configure UiModule in ClientGinjector class.
2) Create a normal presenter (MyPresenter) via GWTP eclipse plugin
3) Change EventBus import this in the presenter
import com.google.web.bindery.event.shared.EventBus;
4) Make sure the MyPresenterView.ui.xml has the following code or similar:
<g:HTMLPanel>
<npui:SimpleTabPanel ui:field="tabPanel" />
<g:SimplePanel ui:field="contentPanel" />
</g:HTMLPanel>
5) Change the presenter to extend TabContainerPresenter instead of Presenter
public class MyPresenter extends
TabContainerPresenter<MyPresenter.MyView, MyPresenter.MyProxy>
6) Define several variables in MyPresenter, or you can just copy/paste the following code:
/**
* This will be the event sent to our "unknown" child presenters, in order
* for them to register their tabs.
*/
#RequestTabs
public static final Type<RequestTabsHandler> TYPE_RequestTabs = new Type<RequestTabsHandler>();
/**
* Fired by child proxie's when their tab content is changed.
*/
#ChangeTab
public static final Type<ChangeTabHandler> TYPE_ChangeTab = new Type<ChangeTabHandler>();
/**
* Use this in leaf presenters, inside their {#link #revealInParent} method.
*/
#ContentSlot
public static final Type<RevealContentHandler<?>> TYPE_SetTabContent = new Type<RevealContentHandler<?>>();
7) Change the constructor of MyPresenter to use the variables:
#Inject
public MyPresenter(final EventBus eventBus, final MyView view, final MyProxy proxy) {
super(eventBus, view, proxy, TYPE_SetTabContent, TYPE_RequestTabs, TYPE_ChangeTab);
}
8) Now we can start to create tab presenters, (e.g MyFirstTabPresenter). Just create a normal presenter again via GWTP eclipse plugin
9) In MyFirstTabPresenter, change MyProxy to let it 'extends' TabContentProxyPlace instead of ProxyPlace
10) Create #TabInfo method, please see javadoc of #TabInfo annotation, you can also use other ways here. For example, I did this:
#TabInfo(container = MyPresenter.class)
static TabData getTabLabel(ClientGinjector ginjector) {
return new TabDataBasic("My First Tab", 0);
}
11) In revealInParent() method of MyFirstTabPresenter class, please make sure it has the following code or similar:
#Override
protected void revealInParent() {
RevealContentEvent.fire(this, MyPresenter.TYPE_SetTabContent, this);
}
That's all related to Tabbed presenter configurations. Now you can add some logic to load some data to show in MyFirstPresenter's view.
I hope this can help you to start with GWTP Tabbed presenter, please let me know any issues you have, I will edit answer gradually and perfect it so that it can help more people to get started with it.
BTW, I also posted this to my blog to help more people on this.
Thanks,
Jiakuan
It doesn't even compile. The only way to trigger multiple presenters in via Nested Presenters - which is tooooo complicated. I built a multiple presenter app with simple GWT History mechanism without any pain. This framework has made GWT History (s aimple mechanism) a very esoteric thing.

Kohana 3 Auto loading Models

I'm attempting to use a Model but I get a fatal error so I assume it doesn't autoload properly.
ErrorException [ Fatal Error ]: Class
'Properties_Model' not found
The offending controller line:
$properties = new Properties_Model;
The model:
class Properties_Model extends Model
{
public function __construct()
{
parent::__construct();
}
}
I also put the class in three different locations hoping one would work, all there failed.
They are:
application/classes/model
application/model
application/models
What am I missing?
Ah, I got this question emailed directly to me (via my website's contact form)!
Here is what I responded with (for the benefit of other people which may run into this problem).
The correct location of a model named
properties is
application/classes/model/properties.php
and the class definition would be as
follows
class Model_Properties extends Model { }
Think of the underscore above as the
directory separator. That is, if you
replaced the underscore with a / you
would have: 'model/properties', which
will be your file under application/classes.
To load the model from a controller,
you can use PHP's standard new
operator or do what I prefer, which is
$propertiesModel = Model::factory('Properties');
I'm not 100% why I prefer this way...
but it works for me :)
First off, The Kohana 3 fileyestem does not work like Kohana 2's!
In K2 the autoloader looks at the class name searches for the class in different folders, based on the suffix of the class.
In K3, class names are "converted" to file paths by replacing underscores with slashes.
i.e. Class Properties_Model becomes classes/properties/model.php
As you can see, using a Model suffix in this new system won't really help to group your models, so basically you prepend "Model" to the class name instead of suffixing it:
Model_Property is located in classes/model/property.php
For more information see the Kohana 3 userguide

Resources