How to load features using composer autoload

I tried adding my traits folder to the composer's compositional startup, but this does not work and returns an error. So is it possible to use startup features with the help of a composer? Thanks so much for any answer.

My trait:

trait User { public function is_email_unique($email) { return $this->User->get_one_by_email($email) ? FALSE : TRUE; } public function is_username_unique($username) { return $this->User->get_one_by_username($username) ? FALSE : TRUE; } } 

My class:

 class Auth extends MY_Controller { // Implement trait in class use User; public function __contstruct() { parent::__contstruct(); } public function register() { if ($this->input->is_post()) { // load validation library $this->load->library('form_validation'); //set validation rules $this->form_validation->set_rules('username', "Username", 'required|callback_username_check'); // There I use my trait method callback_is_email_unique $this->form_validation->set_rules('email', "Email", 'required|valid_email|callback_is_email_unique'); $this->form_validation->set_rules('password', "Password", 'required|matches[confirm_password]|min_length[6]'); $this->form_validation->set_rules('confirm_password', "Confirm password", 'required'); ... } } 

My composer file:

 { "autoload": { "psr-0": { "User": "Validation" } } } 
+8
php traits composer-php autoload
source share
1 answer

I tested back and forth for a while with PHP 5.5.3, and although I have to report that everything looked broken during testing (most likely due to the fact that I made VM with this version in a short time, without settings and wronk keyboard layout), at the end I cannot reproduce the error. I would say that autoload for signs works like an advertisement.

Now here is what I did:

In the home directory:

 composer.json { "autoload": {"psr-0":{"User":"src"}} } 

also:

 test.php <?php require "vendor/autoload.php"; class Auth { use User; } new Auth; 

In the src directory:

 src/User.php <?php trait User {} 

Running composer install creates vendor/composer/autoload_namespaces.php using this array entry:

 'User' => array($baseDir . '/src'), 

And running the script works for me.

Please note that it is important to properly name the files. They must match exactly according to the rules of PSR-0 (or PSR-4 if you prefer to use namespaces), including the correct case-sensitive file names.

If you have a trait named “User” and you define PSR-0 startup as “User”: “Verification”, the expected situation will be that the following file exists: Validation/User.php with content:

 <?php trait User { // stuff here } 

Then this flag will be automatically downloaded after starting at least once composer install .

+4
source share

All Articles