Use custom class in controller

35 views
Skip to first unread message

Muhammad Shahzad

unread,
Apr 1, 2017, 6:14:31 AM4/1/17
to f3-fra...@googlegroups.com

Hi,

I need to use a custom class for csv parsing in my controller action.
Please let me know where should I put that class and then access in my controller.

I have created a directory with name "inc" and put custom class there but when I want to create a object in my controller action it gives me empty screen, I can't see any error.


Directory structure:


app
lib
inc // created this one
db
log
tmp


<?php

namespace Controller;

class ImportCsv extends \Controller {
public function checkerrors($f3){

        $f3->set('DEBUG',3);
        $files = $f3->get('FILES');
        $file = $files['filename_csv'];
        if(!$file['type'] == 'text/csv'){
            $f3->set('message','CSV file allowed only.');
            return false;
        }
        else{
            $f3->set('AUTOLOAD','inc/');
            $objCsvParser = new CsvParser();
            $_SESSION['arrayFeed'] = array();
            $headerSequence=($f3->get('POST.postOrder')?:'');
            $delimeter=($f3->get('POST.delimeter')?:'');
            $objCsvParser->settings = $settings = array(
                'delimiter' => $delimeter,
                'eol' => ";",
                'length' => 999999,
                'escape' => '"');
            $objCsvParser->load($file['tmp_name']);
            $csvHeaders = $objCsvParser->getHeaders();
            $csvValues = $objCsvParser->connect();
            echo "<pre>";
            print_r($csvValues);
            //$_SESSION['CsvParser'] = $csvValues;
        }

    }

}

Thanks

Muhammad Shahzad

unread,
Apr 1, 2017, 6:44:10 AM4/1/17
to Fat-Free Framework
Now getting this error:

Fatal error: Class 'Controller\CsvParser' not found

ved

unread,
Apr 1, 2017, 12:26:31 PM4/1/17
to Fat-Free Framework
If you want to just use that class, instead of changing your autoload variable you can just use require 'path/to/file.php' before initializing it.

Also, assuming your app folder is subdivided into controller,models,view folders or something similar, you can create some kind of "helper" folder and namespace your class and then use "new \Helper\MyClass();" inside your controllers.
Reply all
Reply to author
Forward
0 new messages