📜 ⬆️ ⬇️

Packages in Fuelphp

In this article I would like to share knowledge about how they are arranged, what they are for, and what packages are (further packages) in Fuelphp . I ask you to help compare the implementation and capabilities of Fuelphp packages with counterparts from other frameworks. I think everyone will be interested to know the opinion of adherents of Simfony2 and Yii on this issue, you are welcome, throw your calculations in the comments. The examples below will consider the fuel-ninjauth package.

Assigning packages in the context of Fuelphp

To perform these or other recurring global tasks, in an application, an approach using services or services has been used for a long time, which is essentially the same thing. If simple, then the service is an object that performs some global task in the context of the application. In Fuelphp, the role of such services is performed by packages.

Package installation

Packages can be shared with the community. There are several ways to install packages, you can simply copy the contents of the repository to your project, use composer or use the utility oil .

Refer to the oil with the command:
$ php oil package install ninjauth 

Perform the migration, as this package uses data from the database:
 #   users     $ oil g migration create_users username:varchar[50] password:string group:int email:string last_login:integer login_hash:string profile_fields:text created_at:int $ oil refine migrate #        "authentications" $ oil refine migrate --packages=ninjauth 

Package structure

Consider a possible structure using the example of the fuel-ninjauth package.
image
1. Package Classes
The classes folder contains the package classes. Example
')
1.1 Package Models
Models can also be part of package classes when a package uses a database. Example

1.2. Controllers
Controllers in packages are used less frequently and are mainly used to form representations within the package. Example As a result, the entire controller logic can be reduced to something like this:
 return View::forge('register', array( //register -     №4 views 'user' => (object) compact('username', 'full_name', 'email', 'password') )); 

2. Configuration
Configuration or configuration file. Example
An example of addressing the settings:
 \Config::get('ninjauth.default_group'); 

3. Migrations
This folder contains the classes of migrations that are needed to quickly assemble the data structure in the database, create the necessary tables, etc.

4. Representations
Package can use views. As a rule, the package processes the logic and is not related to the formation of the representation directly, but sometimes it may turn out that the formation of the representation inside the package will be a good solution.
Sample view:
 <?php echo Form::open(null, array('id' => 'register')); ?> <?php if (isset($error)): ?> <span class="error"><?php echo $error; ?></span> <?php endif; ?> <p> <label for="username">Username</label> <?php echo Form::input('username', $user->username) ?> </p> <p> <label for="full_name">Full Name</label> <?php echo Form::input('full_name', $user->full_name) ?> </p> <p> <label for="email">Email</label> <?php echo Form::input('email', $user->email) ?> </p> <p> <label for="password">Password</label> <?php echo Form::password('password') ?> </p> <?php echo Form::submit('submit') ?> <?php echo Form::close() ?> 

5. The bootstrap file.
This file is used to describe the file structure of the package for the autoloader.
 Autoloader::add_classes(array( 'NinjAuth\\Controller' => __DIR__.'/classes/controller.php', 'NinjAuth\\Exception' => __DIR__.'/classes/exception.php', 'NinjAuth\\CancelException' => __DIR__.'/classes/exception.php', 'NinjAuth\\ResponseException' => __DIR__.'/classes/exception.php', 'NinjAuth\\Model_Authentication' => __DIR__.'/classes/model/authentication.php', 'NinjAuth\\Strategy' => __DIR__.'/classes/strategy.php', 'NinjAuth\\Adapter' => __DIR__.'/classes/adapter.php', 'NinjAuth\\Adapter_SimpleAuth' => __DIR__.'/classes/adapter/simpleauth.php', 'NinjAuth\\Adapter_Sentry' => __DIR__.'/classes/adapter/sentry.php', 'NinjAuth\\Adapter_Warden' => __DIR__.'/classes/adapter/warden.php', 'NinjAuth\\Strategy_OAuth' => __DIR__.'/classes/strategy/oauth.php', 'NinjAuth\\Strategy_OAuth2' => __DIR__.'/classes/strategy/oauth2.php', 'NinjAuth\\Strategy_OpenId' => __DIR__.'/classes/strategy/openid.php', )); 

It is important to understand that files are loaded automatically, and only in the case of using package methods. In other words, if the package methods were not used anywhere in the script development, then its classes will not be loaded. It makes your application easier and faster. The only thing the application always knows is the location of the bootstrap.php files:
image

Examples of using

The NinjAuth package is an adapter for the standard autoization package and it will be difficult to make a simple example of its operation. Therefore, take the package fueltools . It is needed to debug the code when you need to quickly see what the method or variable returns. So, let's say we write the hundredth method of our application:
 public function action_save() { $var1 = 'var1'; $var2 = 'var2'; $orm = Model_Test::forge()->set('one', $var1)->set('two',$var2); \Fueltools\FB::info($var1); \Fueltools\FB::info($var2); \Fueltools\FB::info($orm->save()); } 

See the result of executing this method in the Firefox console: image
Fuelphp loaded only those classes of the ORM and FuelTools packages that were required for the development of the script:
image

Results

Fuelphp provides in the form of packages quite ample opportunities:
- formation of an orderly and understandable application architecture
- use of services, as in the example with fueltools
- the use of patterns and the connection of additional functionality to existing packages, as in the fuel-ninjauth package, which is an adapter to the standard Auth package.
- Packages allow you to take on the functions of the formation of representations, and if desired, in conventional controllers you can leave only checks and I / O, transferring everything else to the packages.
- packages allow you to abstract from other code in the application, it is easier to test and connect in another project
- package objects and methods become available in the context of the entire application
- using packages makes your application easier and faster.

Src

- fuel-ninjauth package
- fueltools package
- official package repository for Fuelphp github.com
- repository on the forum fuel-packages

Source: https://habr.com/ru/post/156449/


All Articles