CodeIgniter Forums

Full Version: [SOLVED] CI4 - Unable to Autoload Markdown Class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I am in the process of trying to use CI4 for an existing PHP program that utilises MarkDown.

In accordance with the CI4 Manual I have added the following to /app/Config/Autoload.php
PHP Code:
        /* Files available:
$fff '/var/www/ci2/php-markdown/Michelf/';    
$classmap = [
        'Markdown' => $fff
// $classmap = []; // ORIGINAL SCRIPT 

Unfortunately I am unable to use CI4 way of using Markdown

I can only get it to work using require_once('Class_MarkDown.php); ... can be seen in the following class:
PHP Code:


# use App\Models\UserModel;

require_once '/var/www/ci2/php-markdown/Michelf/';
// use Markdown, Michelf\SmartyPants;
// use Markdown, SmartyPants;
// use Markdown;

# ==========================================================
class Home extends BaseController

# ==========================================================
public function index()
 // $fff = file_get_contents(WRITEPATH .'markdown/');
 $fff  '## Markdown test to see if H2 works OK';

 $txt Markdown::defaultTransform($fff);
'<div class="tal bd4 p42">'

 $data['header'] = $this->header();
 $data['body'  $body;
 $data['footer'] = $this->footer();


# ========================================================== 
Did you install markdown using Composer? Is so, and if I understand correctly, then you do not need to add anything to /app/Config/Autoload.php. Doing so may be the source of your problem.

CI4 is hardwired to use the composer autoloader which will work just fine in conjunction with the C4 autoloader.

Using require_once is only appropriate when not using an autoloader.
I did not use Composer for the installation.

I downloaded and saved Markdown into my common library folder.

I expected when running CI4 it would autoload the required Markdown library and I would be able to use the Markdown methods.
If you are going to copy files into the app/Libraries folder, they might need to be namespaced App\Libraries in order for CI4 to find them automatically.
> @ ciadmin
> If you are going to copy files into the app/Libraries folder, they might need to be namespaced App\Libraries in order for CI4 to find them automatically.

Many thanks, it is now too late for me to try tonight, looking forward to trying tomorrow.
Unfortunately I could not get namespace to work and I did try for well over an hour Sad

I would be grateful if someone could supply a very simple example of the correct way to use config/Autoload.php and to set the Markdown namespace and use statement.

Girhub Repository:
The problem is that the files themselves have a namespace, Michelf. So the class Markdown doesn't actually exist.

The simplest way would be composer. If you don't want to use that you can use CI's autoload and let it know the namespace and where it's located. In app/Config/Autoload.php:

$psr4 = [
    'Config'      => APPPATH . 'Config',
    'App'         => APPPATH,          
    'Michelf' => '/var/www/ci2/php-markdown/Michelf/'
@ kilishan - Many thanks for the prompt reply. 

I am now able correctly to use the library.

Perhaps amend the CI4 User Guide would prevent others making the same mistake.
For those who would like to try the following Markdown Github Repository:

1. Github clone or download into a \any\folder\of\your\choice\ 

2. Amend this file: app/Config/Autoload.php
PHP Code:
// $psr4 = [
//    'Config'      => APPPATH . 'Config',
//    APP_NAMESPACE => APPPATH,                // For custom namespace
//    'App'         => APPPATH,                // To ensure filters, etc still found,
// ];

// NEW
$psr4 = [
   'Config'      => APPPATH 'Config',
   'App'         => APPPATH         
'Michelf' => '\any\folder\of\your\choice\php-markdown/Michelf/'

3. Markdown Class:
PHP Code:


   Michelf\SmartyPants// NOT REQUIRED

# ==========================================================
class Home extends BaseController

# ==========================================================
public function index()
   # DEMO
   $tmp  '#        Markdown H1'
         "\n##     Markdown H2"
         "\n###    Markdown H3"
         "\n####   Markdown H4"
         "\n#####  Markdown H5"
         "\n###### Markdown H6"
         "\nMarkdown paragraph stuff goes here"
         "\n\t 1. Order list one"
         "\n\t 2. Order list two"
         "\n\t 3. Order list three"
   $tmp Markdown::defaultTransform($tmp);
   echo $tmp;

# USING Markdown files
   $data['header'] = $this->header();
   $top            file_get_contents(WRITEPATH .'markdown/');
   $body           file_get_contents(WRITEPATH .'markdown/');
   $footer         file_get_contents(WRITEPATH .'markdown/');

     $md = new Markdown;
     $data['top'   $md->defaultTransform($top);
     $data['body'  $md->defaultTransform($body);
     $data['footer'] = $md->defaultTransform($footer);

     $data['top'   Markdown::defaultTransform($top);
     $data['body'  Markdown::defaultTransform($body);
     $data['footer'] = Markdown::defaultTransform($footer);

    return view

Thanks for the follow up to your experience! I’d also add that since this ultimately was a namespace mistake, really the best way to use it in CI4 is to install it via Composer:

cd CI/root/dir
composer require michelf/php-markdown

And that’s it! There may be reasons OP and others would want a separate folder, but this is really the way CI4 was designed.
Pages: 1 2