How can I extend more than only MY_Controller with Composer? |
Hello,
I was trying to create two base Controllers depending on whether the user is logged in or not. I already tried with Phil Sturgeon's solution https://philsturgeon.uk/blog/2010/02/Cod...ng-it-DRY/ and works without a problem (my tutorial on this is here: http://avenir.ro/codeigniter-tutorials/n...ase-class/). But I was wondering if there is a way to do this by using Composer. So, at first I tried doing it with file based autoloading. I enabled composer autoloading inside config.php: $config['composer_autoload'] = TRUE; I created a base controller named Admin_Controller.php: PHP Code: <?php After that, I created a composer.json inside application: Code: { And after that I did an extend in the Welcome Controller PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); ...but it doesn't seem to work... Now, if I instead extend CI_Controller and look at the loaded classes, I see that the Admin_Controller is loaded: PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); So, where did I did wrong? Could someone help me? Website: http://avenir.ro
(12-08-2014, 08:02 AM)Avenirer Wrote: Hello, Try to move your Admin_Controller.php from core to library.... You could also try a spl_autoload_register in your config file like this: PHP Code: function app_lib($classname) {
Romanian CodeIgniter Team :: Translations :: Comunity :: Developers
http://www.codeigniter.com.ro
Please don't use my solution.... i see you already used Phil Sturgeon's solution.
I personally create a MY_Controller.php in the core directory and than Admin_Controller.php and Site_Controller.php in library directory. An of course, the spl_autoload_register in config file...i never used the composer for autoloading classes.
Romanian CodeIgniter Team :: Translations :: Comunity :: Developers
http://www.codeigniter.com.ro
just need to ask which config file you are putting this code config.php or any other file what if we put in index.php file.
(12-09-2014, 06:07 AM)Dracula Wrote:
Here is our autoloader that goes in the config.php file at the very bottom.
PHP Code: /* What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
See this post by Lonnie Ezell http://forum.codeigniter.com/thread-420-...ml#pid2665
(12-22-2014, 09:13 AM)ivantcholakov Wrote: See this post by Lonnie Ezell http://forum.codeigniter.com/thread-420-...ml#pid2665 Well... I did try that, but still no luck, that is why I tried to get back to the basics... with psr-0. And it didn't work ![]() Website: http://avenir.ro
(12-22-2014, 03:37 PM)Avenirer Wrote:(12-22-2014, 09:13 AM)ivantcholakov Wrote: See this post by Lonnie Ezell http://forum.codeigniter.com/thread-420-...ml#pid2665 He only gives the clue without details. But he has published code that covers exatly the topic you are working on. Here are his base controllers https://github.com/ci-bonfire/Sprint/tre...ontrollers and here is his Composer.json https://github.com/ci-bonfire/Sprint/blo...poser.json
I have an update on the problem. It seems to me that the "composer_autoload" parameter in the config file is somehow broken (on first view). This is what I did now:
1. I created a parent directory where I put my base controllers inside application ('application/avenirer/BaseControllers'). 2. In BaseControllers directory I create a file named Admin_Controller.php: PHP Code: <?php namespace Avenirer\BaseControllers; 3. I create a composer.json inside application (added a require php 5.4 for namespacing sake. is not really necessary for that line... Don't worry about namespacing, I tried with classmap too, and I have the same problem...): Code: { 4. I do a "composer install" inside application so that the autoloaders are created. Now, if I look inside 'application/vendor/composer/autoload_psr4.php' I will see: PHP Code: return array( 5. Next step would be to require_once the composer's autoloader. For explanation sake I will do that inside the application/config/config.php file, just below the composer_autoload config item (making sure that the parameter is set to FALSE): PHP Code: $config['composer_autoload'] = FALSE; 6. Now I create a controller and do an extend for Admin_Controller: PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); 7... And I get a 'hello' from my Admin_Controller. Conclusion: it works. Questions to be answered: If instead, I set composer_autoload parameter to TRUE and comment the autoload.php I put earlier...: PHP Code: $config['composer_autoload'] = TRUE; ... it doesn't work ![]() Now, to verify if the autoload.php was loaded, instead of extending Admin_Controller, I will extend CI_Controller. The print_r() function show me that the files were loaded corectly. But when I extend Avenirer\BaseControllers\Admin_Controller I get the same Fatal error: Class 'Avenirer\BaseControllers\Admin_Controller' not found in... What is wrong here? Website: http://avenir.ro
So many different solutions. I don't use any of them.
This is what I do. in index.php: Quote:/* Lets say I'm going to use rabbitmq. In my rabbitmq function library: Quote:<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Done... ![]() In fact, I have multiple composer libraries that I use. None of them are loaded into the Base Controller. All my composer libs are in code igniter libs that I have created. So when I want to use a composer lib, I load up the CI lib, do what I need to do and that's it. No messing with namespaces, no messing with the Base Controller. Nice and easy.... ![]() |
Welcome Guest, Not a member yet? Register Sign In |