Welcome Guest, Not a member yet? Register   Sign In
Codeigniter and Composer psr-4
#1

Hi guys
composer.json
Code:
{
 "autoload": {
   "psr-4": {
     "App\\": "application/libraries"
   }
 }
}

application/libraries
PHP Code:
<?php
namespace App\Libraries;

class 
Welcome_library {

 
   public function __construct()
 
   {
 
       echo 'hi';
 
   }


application/controllers
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Welcome extends CI_Controller {

 
   /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *        http://example.com/index.php/welcome
     *    - or -
     *        http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
 
   public function index()
 
   {
 
       new App\Libraries\Welcome_library;
 
       $this->load->view('welcome_message');
 
   }



PHP Code:
Fatal error: Class 'App\Libraries\Welcome_library' not found 

What am I doing wrong.
Thanks
Reply
#2

Not actually using Composer's autoloader?
Reply
#3

(03-14-2017, 11:11 AM)Narf Wrote: Not actually using Composer's autoloader?

In config set composer autoload true php 5.5 codeigniter 3.1.3. Composer installing.
Reply
#4

(03-14-2017, 11:11 AM)Narf Wrote: Not actually using Composer's autoloader?

In config set composer autoload true php 5.5 codeigniter 3.1.3. Composer installing. You talk about it
require __DIR__ . '/vendor/autoload.php
Reply
#5

Setting it to TRUE means that it will look for application/vendor/autoload.php
Is that the correct path for your setup?

If not, set it to the path to the autoload.php file.
Reply
#6

(03-15-2017, 02:57 AM)Narf Wrote: Setting it to TRUE means that it will look for application/vendor/autoload.php
Is that the correct path for your setup?

If not, set it to the path to the autoload.php file.

Thanks Narf. I just forgot to add to the index.php

PHP Code:
require FCPATH.'vendor'.DIRECTORY_SEPARATOR.'autoload.php'
Reply
#7

You don't need to add it to index.php ...
Reply
#8

(This post was last modified: 03-16-2017, 04:52 AM by llebkered.)

There are 2 ways. One is a simple change to your config. The other is including a link in your index.php. Changing the config is the best because it doensn't breaak the coding commandment: "Thou shalt not hack thine core".

Method 1:
Change the following in application/config/config.php.

$config['composer_autoload'] = './vendor/autoload.php'; (or where ever your autoload.php is located)

Method 2:
You can also add include_once './vendor/autoload.php'; to index.php as per Phil Sturgeon's blog https://philsturgeon.uk/php/2012/05/07/c...deigniter/
Reply
#9

Removed from index.php and set in config $config['composer_autoload'] = 'vendor/autoload.php';
Everything works fine, thanks. This is all my inattention.
Reply
#10

(03-16-2017, 04:51 AM)llebkered Wrote: There are 2 ways. One is a simple change to your config. The other is including a link in your index.php. Changing the config is the best because it doensn't breaak the coding commandment: "Thou shalt not hack thine core".

Method 1:
Change the following in application/config/config.php.

$config['composer_autoload'] = './vendor/autoload.php'; (or where ever your autoload.php is located)

Method 2:
You can also add  include_once './vendor/autoload.php'; to index.php as per Phil Sturgeon's blog https://philsturgeon.uk/php/2012/05/07/c...deigniter/

Thanks, understood
Reply




Theme © iAndrew 2016 - Forum software by © MyBB