Skip to content

Latest commit

 

History

History
597 lines (300 loc) · 14.3 KB

parent-class.md

File metadata and controls

597 lines (300 loc) · 14.3 KB

Parent classes

AppController

View

...

Libs of KumbiaPHP

Kumbiaphp have ready-to-use classes, but remember that you can create your own classes to reuse them in your projects. You can also use external KumbiaPHP classes, as explained in the next chapter.

Cache

A cache is a set of duplicate data of other originals, with the property that the original data are costly to access, usually in time, with respect to the copy in the cache.

The cache data is implemented in KumbiaPHP using factory and singleton design patterns. To make use of the cache, it is necessary to have write permissions in the "cache" directory (only in the case of the handlers "sqlite" and "file").

Each cache is controlled by a cache handler. The system cache KumbiaPHP has currently the following handlers:

  • APC: use Alternative PHP Cache.
  • file: cache files, these are stored in the directory cache and compatible with all operating systems.
  • nixfile: cache files, these are stored in the directory cache and compatible only with operating systems *nix (linux, freebsd, and others). This cache is more fast cache «file».
  • sqlite: cache using SqLite database and this is located in the cache directory.

To obtain a cache handler you must use the method «driver» which provides the Cache class.

driver($driver=null)

This method allows to obtain a cache handler specific (APC, file, nixfile, sqlite, memsqlite). If not indicated, gets cache handler by default indicated in the config.ini.

get ( 'data' ); // manejador para memcache $data_memcache = Cache:: driver ( 'memcache' )-> get ( 'data' ); // manejador para cache con APC $data_apc = Cache:: driver ( 'APC' )-> get ( 'data' ); \--- ### get($id, $group='default') Permite obtener un valor almacenado en la cache; es necesario especificar el parámetro $id con el "id" correspondiente al valor en cache, tomando de manera predeterminada el grupo "default". ### save($value, $lifetime=null, $id=false, $group='default') Permite guardar un valor en la cache, el tiempo de vida del valor en cache se debe especificar utilizando el formato de la funcion \[strtotime\](http://www.go ogle.com/url?q=http%3A%2F%2Fphp.net%2Fmanual%2Fes%2Ffunction.strtotime.php&sa= D&sntz=1&usg=AFQjCNH8Gfguulpd1VXJunl_FfDbd4Mc8w) de php. Al omitir parámetros al invocar el método save se comporta de la manera siguiente: * Si no se especifica $lifetime , entonces se cachea por tiempo indefinido. * Si no se especifica $id y $group , entonces se toma los indicados al invocar por última vez el método get . get('saludo'); if(!$data) { Cache::driver()->save('Hola', '+1 day'); } echo $data; \--- ### start ($lifetime, $id, $group='default') Muestra buffer de salida cacheado, o en caso contrario inicia cacheo de buffer de salida hasta que se invoque el método end. Este método se utiliza frecuentemente para cachear un fragmento de vista. start('+1 day', 'saludo')): ?>
Hello <? php echo $user? > <? php Cache:driver()-> end()? >

end ($save = true)

End caching output buffer indicating if it should be kept or not in the cache.


Logger

The Logger class for handling [Log] (http://www.google.com/url?q=http%3A%2F %2Fes.wikipedia.org%2Fwiki%2FLog_(registro) & sa = D & sntz = 1 & usg = AFQjCNGft16YEbrl ayLoKbZFpNDBDXgXAA) was rewritten statically, this means no longer necessary to create an instance of the Logger class. This class offers a variety of methods to handle different types of Log.


The previous statement will output the following:

[Thu, 05 Feb 09 15:19:39-0500][ERROR] Error message

By default the log files have the name logDDMMYYY.txt this name can be changed if we so wish it through an additional parameter to the method.


You can see the second parameter now the file will be named mi_log.txt

Logger:warning ($msg);

Logger:error ($msg)

Logger:debug ($msg)

Logger:alert ($msg)

Logger: critical ($msg)

Logger:notice ($msg)

Logger:info ($msg)

Logger::emergence ($msg)

Logger::custom ($type = 'CUSTOM', $msg)


Flash

Flash is a very useful helper in Kumbia that makes the messages of error, warning, informational and success output as standard.

Flash::error ($text)

— Send an error message to the user. Default is a message of letters red and pink background but these can be altered in the cascading style sheet class in public /css/style.css called error.

Flash::error ("an error occurred");


Flash::valid ($text)

It allows to send a success message to the user. Default is a lyrics color message green and pastel green color background but these can be altered in the cascading style sheet class in public/css/style.css called valid.

Flash: valid("Realized the process correctly");


Flash:info ($text)

It allows you to send information to the user. Default is a message of letters color blue and blue pastel-coloured background; but these can be altered in the cascading style sheet class in public/css/style.css called info.

Flash::info("There are no results in the search");


Flash:warning ($text)

It allows to send a warning message to the user. Default is a message of letters color blue and pastel blue color background but these can be altered in the cascading style sheet class in public/css/style.css called warning.

Flash:warning("Warning:_you_are_not_login_in_the_system");


Flash::show($name, $text)

...


Session

The Session class is to facilitate handling of the session.

Session:set($index,$value,$namespace='default')

Create or specify the value for an index of the current session.


Session: get ($index, $namespace ='default ')

Get the value for an index of the current session.


Session:delete($index,_$namespace='default')

Deletes the value for an index of the current session.


Session:has($index,_$namespace='default')

Verify that this set the rate at the current session.


Note: $namespace is a single space in which may contain the session variables, avoiding collisions with variable names.

Load

Class load allows the loading of libraries in KumbiaPHP.

Load::coreLib ($lib)

It allows to load a KumbiaPHP core library.

<input name="mode" type="hidden" value="auth">

<label for="login">User:</label>

<?php echo Form::text('login') ?>

<label for="password">Clave:</label>

<?php echo Form::pass('password') ?>

Default Auth2 takes the "login" field for the user name and the password 'password' field.

To be able to start a user session and perform the authentication you must invoke the identify method, however depending on the adapter type, it is necessary to specify certain configuration settings.

Adapter Model

This adapter allows you to use authentication based on a model which inherits from the ActiveRecord class, verifying authentication at the database data.

setModel()

Sets the ActiveRecord model that is used as a data source. By default the model that is used as a data source is 'users'.

$model (string): model name in lowercase

setModel ($model)

Example:

$auth->setModel ('user');


identify()

Performs the authentication. If there is already an active user session or user data are correct, then the identification is satisfactory.

return true or false

identify()

Example:

$valid = $auth->identify();


logout()

End of the user session.

logout()

Example:

$auth->logout();


setFields()

Sets the fields of the model that are loaded into session using the Session:set method. By default load the field 'id'.

$fields (array): array fields

setFields ($fields)

Example:

$auth->setFields (array ('id', 'user'));


setSessionNamespace()

Sets a namespace for fields that are loaded into session.

$namespace (string): session namespace

setSessionNamespace($namespace)

Example:

$auth->setSessionNamespace ('auth');


isValid()

Check if there is an authenticated user session.

return true or false

isValid()

Example:

$valid = $auth->isValid();


getError()

Gets the error message.

return string

getError()

Example:

if(!$auth->identify()) flash:error ($auth->getError());


setAlgos()

Sets the user password encryption method.

$algos (string): method of encryption, the name matches the php hash function.

setAlgos ($algos)

Example:

$auth->setAlgos ('md5');


setKey()

Sets the key to identifying whether a session authenticated, this key takes a Boolean value of "true" when the authenticated session is valid, assigned by using the Session:set method.

$key (string): session key

setKey ($key)

Example:

$auth->setKey('user_logged');


setCheckSession()

It indicates that not start session from a different browser on the same IP.

$check (boolean): indicator

setCheckSession ($check)

Example:

$auth->setCheckSession(true);


setPass()

Assigns the name of field for the key field. This field must correspond with the data base and the form field. Default is "password".

$field (string): name of field that receives by POST.

setPass($field)

Example:

$auth->setPass('key');


setLogin()

Assigns the name of the field for the user name field. This field must correspond with the data base and the form field. Default is "login".

$field (string): name of field that receives by POST.

setLogin ($field)

Example:

$auth->setLogin ('user');


Get the fields loaded on session

The fields are obtained through the Session:get method.

$id = Session::get('id');


If you have specified a namespace of session, you must then specify it by invoking the method.

$id = Session::get('id', 'my_namespace');


Example

The view:

app/views/access/login.phtml

<input name="mode" type="hidden" value="auth">

<label for="login">User:</label> 

<? php echo Form:text('login')? >

<label for="password">Password:</label>

<? php echo Form:pass('password')? >

Controller:

app/controllers/auth_controller.php

login()) { Router::toAction('usuario/panel'); } } public function logout() { // Termina la sesion Load::model('usuario')->logout(); Router::toAction('login'); } } ?>

To validate this authenticated the user, just add the following code in any controller action or method before_filter:

if(!Load::model('user')->logged()) {

Router:toAction('auth/login');


return false;

}


Model:

app/models/usuario.php

setModel ('user'); if($auth->identify()) return true; Flash:error($auth->getError()); return false; } / * * End * * / public function logout() {Auth2:factory('model') - > logout();} } / * * Check if this authenticated user * @return boolean * / public function logged() {return Auth2:factory('model') - > isValid();} } }