CodeIgniter Forums
[SOLVED] CI4 - Unable to Autoload Markdown Class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: [SOLVED] CI4 - Unable to Autoload Markdown Class (/thread-73458.html)

Pages: 1 2


[SOLVED] CI4 - Unable to Autoload Markdown Class - John_Betong - 04-26-2019

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:
            Markdown.inc.php
            Markdown.php
            MarkdownExtra.inc.php
            MarkdownExtra.php
            MarkdownInerface.inc.php
            MarkdownInerface.php
        */
        
$fff '/var/www/ci2/php-markdown/Michelf/MarkdownExtra.inc.php';    
        
$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); ...

...as can be seen in the following class:
PHP Code:
<?php 
declare(strict_types=1);

namespace 
App\Controllers;

use 
CodeIgniter\Controller;
# use App\Models\UserModel;

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

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

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

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

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

    return 
view('v_home'$data);
}
//


}///
# ========================================================== 



RE: CI4 - Unable to Autoload Markdown Class - dave friend - 04-26-2019

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.


RE: CI4 - Unable to Autoload Markdown Class - John_Betong - 04-26-2019

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.


RE: CI4 - Unable to Autoload Markdown Class - ciadmin - 04-26-2019

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.


RE: CI4 - Unable to Autoload Markdown Class - John_Betong - 04-26-2019

> @ 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.


RE: CI4 - Unable to Autoload Markdown Class - John_Betong - 04-26-2019

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:

https://github.com/michelf/php-markdown


RE: CI4 - Unable to Autoload Markdown Class - kilishan - 04-26-2019

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:

Code:
$psr4 = [
    'Config'      => APPPATH . 'Config',
    APP_NAMESPACE => APPPATH,
    'App'         => APPPATH,          
    'Michelf' => '/var/www/ci2/php-markdown/Michelf/'
];



RE: CI4 - Unable to Autoload Markdown Class - John_Betong - 04-27-2019

@ kilishan - Many thanks for the prompt reply. 

I am now able correctly to use the https://github.com/michelf/php-markdown library.

Perhaps amend the CI4 User Guide would prevent others making the same mistake.

https://codeigniter4.github.io/CodeIgniter4/concepts/autoloader.html?highlight=markdown


RE: CI4 - Unable to Autoload Markdown Class - John_Betong - 04-27-2019

For those who would like to try the following Markdown Github Repository:

https://github.com/michelf/php-markdown

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


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

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



3. Markdown Class:
PHP Code:
<?php 
declare(strict_types=1);

namespace 
App\Controllers;

use 
CodeIgniter\Controller,
 
   Michelf\Markdown
 
   Michelf\SmartyPants// NOT REQUIRED

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

# ==========================================================
public function index()
{
 
 if(0):
 
   # 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"
 
         "\n\t 1. Order list one"
 
         "\n\t 2. Order list two"
 
         "\n\t 3. Order list three"
 
         "\n"
 
         ;
 
   $tmp Markdown::defaultTransform($tmp);
 
   echo $tmp;
 
   exit;

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

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

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

    return view
('v_home'$data);
 
 endif 
}//


}/// 



RE: [SOLVED] CI4 - Unable to Autoload Markdown Class - MGatner - 04-28-2019

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.