Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] CI4 - Unable to Autoload Markdown Class
#1

(This post was last modified: 04-27-2019, 11:58 PM by John_Betong.)

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);
}
//


}///
# ========================================================== 
Reply
#2

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.
Reply
#3

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.
Reply
#4

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.
Reply
#5

(This post was last modified: 04-26-2019, 09:50 AM by John_Betong.)

> @ 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.
Reply
#6

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
Reply
#7

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/'
];
Reply
#8

(This post was last modified: 04-27-2019, 09:11 PM by John_Betong.)

@ 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/CodeIgnit...t=markdown
Reply
#9

(This post was last modified: 04-27-2019, 09:31 PM by John_Betong.)

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 
}//


}/// 
Reply
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB