Codeigniter Fatal error when calling css less functions - php

I have a problem when I call this function I am getting the following error:
"Fatal error: Uncaught exception 'Exception' with message 'load error: failed to find test.less' in C:\xampp\htdocs\project\application\views\lessc.inc.php:1818"
<?php
require('lessc.inc.php');
$less = new lessc;
echo $less->checkedCompile("test.less","hoba.css");
?>
and I have test.less, hoba.css and lessc.inc.php all in the views folder

Try,
<?php
require('lessc.inc.php');
$less = new lessc;
echo $less->checkedCompile(site_url()."application/views/test.less",site_url()."application/views/hoba.css");
?>

I'm not familiar with the lessphp class, but try putting the "less.inc.php" (and any other files associated with it) in application/third_party. Next create a file "less.php" in application/libraries. In that file, paste the following:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/less.inc.php";
class Less extends lessc {
public function __construct() {
parent::__construct();
}
}
In the controller where you need it, something like:
$this->load->library('less');
$data['css'] = $this->less->checkedCompile(base_url()."path/to/test.less",base_url()."path/to/hoba.css");
$this->load->view('your_view', $data);
Also, it looks like you're storing .less and .css files in the application/views folder. You should store these outside the application folder, in a folder called "assets/css" or something similar.

Related

How to call a function in helper from outside codeigniter (ci) - tried the solution from other stackoverflow links but doesn't work

I have created a function in application/helpers/ which works fine when accessing from ci.
Now I want to call the same function from outside ci i.e., from core PHP file. But I'm not able to do so.
Below is the approach tried.
Created a test.php outside ci and below is the code:
<?php
$filepath = dirname(__FILE__);
ob_start();
require_once($filepath.'/ci/index.php');
ob_get_clean();
return $CI;
?>
In another core PHP file, test2.php, below is the code:
$CI = require_once('test.php');
echo $CI->config->item('base_url');
some_helper_function($param1, $param2);
Error message:
Fatal error: Call to a member function item() on a non-object in <path>/Utf8.php on line 47
Folder structure:
test.php
test2.php
ci/application/helpers/test_helper.php (contains some_helper_function())
Any suggestions?
Create a file and put the following code into it.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('test_function'))
{
function test_function()
{
//your functionality//
}
}
Save this to application/helpers/ . save it as "test_helper.php"
The first line exists to make sure the File cant be included and run from outside the Code Igniter.
Then in your controller or Model
$this->load->helper('test_helper');
You can use any function from that helper page.
Else
your-project-name\application\config\autoload.php
$autoload['helper'] = array('test_helper');
So that you can use that helper function in any controller or model. Not to initialize in each and every controller or model like previously said.

CodeIgniter does not autoload my core extension

As I am very pleased of the command show_404() which you can call everywhere to show a 404-Error-Page, I did want to implement a show_403() for requests without permissions.
I created the file application/core/MY_Exceptions.php and added the following code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Exceptions extends CI_Exceptions {
public function __construct()
{
parent::__construct();
}
public function show_403($page = '', $log_error = TRUE)
{
//do some stuff
echo "test";
}
}
Then I'll call it in a controller application/controllers/Welcome.php like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
// show_404(); // <-- this works!
show_403(); // <-- this works not!
}
}
and I keep getting the following error in the browser, when I access the controllers index method:
Fatal error: Call to undefined function show_403()
As you may have noticed I even tested this on a vanilla installation of CodeIgniter, so you should be able to reproduce this error with just these two files.
I know that I could load the extension manually, but that has not the elegance of using show_403() wherever and whenever I want...
Routing is set up correctly, CodeIgniter is version 3.0.3, PHP is version 5.6.12., filesystem permissions to application/core/MY_Exceptions.php have even been set to 777 for debugging purposes.
Your exception class is not loaded, so its giving that error.
Load your class like this
$excep = load_class('Exceptions', 'core', $this->config->item('subclass_prefix'));
echo $excep->show_403();// will echo test
One more suggesion is to use helpers for this,
Add your code in application/helpers with file name error_helper
public function show_403($page = '', $log_error = TRUE)
{
//do some stuff
echo "test";
}
Calling your function
$this->load->helper('error_helper');
echo show_403();

Fatal error: Cannot use object of type Template as array

Having a bit of an issue with Codeigniter - I am trying to build a module for a CMS system, but I get the following issue:
Fatal error: Cannot use object of type Template as array in (baseURL) on line 16
The code is as followed:
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Forum extends CI_Controller {
function Forum()
{
parent::__construct();
$this->template['module'] = 'forum';
$this->load->model('forum_model', 'forum');
$this->load->model('topic_model', 'topic');
$this->load->model('message_model', 'message');
$this->load->library("bbcode");
$this->forum->get_user_level();
$this->plugin->add_action('header', array(&$this, '_write_header'));
}
}
You've got a library named 'template' that is auto-loaded.
Libraries are objects, and you're trying to use this one as an array, which is what the error message is telling you.

CodeIgniter PHPWord using as third_party

I am trying to use PHPWord and having a difficulties to use it.
This my code within application/libraries/words.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/PhpWord/Autoloader.php";
\PhpOffice\PhpWord\Autoloader::register(); \\NOT SURE IF THIS IS CORRECT
class Word extends PhpWord {
public function __construct() {
parent::__construct();
}
}
Based on PHPWord github, i must use it like this:
Alternatively, you can download the latest release from the releases page. In this case, you will have to register the autoloader.
require_once 'path/to/PhpWord/src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register(); // WHERE SHOULD I PUT THIS?
When i try to load the library from my controller as such:
$this->load->library('word');
it gives me error that says:
Fatal error: Class 'PhpWord' not found in C:\xampp\htdocs\path\to\application\libraries\Word.php on line 7
i tried to extends the Autoloader class, but PHP still complains that it can't found Autoloader.php.
when i do
file_exist(APPPATH."/third_party/PhpWord/Autoloader.php") // Similarly with PhpWord.php
it returns 1.
Does anyone know how to fix this?
From Russia with love;)
Use like this in library for CodeIgniter:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH.'/libraries/PHPWord/src/PhpWord/Autoloader.php';
use PhpOffice\PhpWord\Autoloader as Autoloader;
Autoloader::register();
class Word extends Autoloader {
}
?>
In controller like code from samples:
$this->load->library('word');
/* remove ;)
#require_once 'PHPWord/src/PhpWord/Autoloader.php';
#PhpOffice\PhpWord\Autoloader::register();
*/
// Template processor instance creation
echo date('H:i:s') , ' Creating new TemplateProcessor instance...' ;
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('text.docx');
$templateProcessor->setValue('sss','123');
echo date('H:i:s'), ' Saving the result document...';
$templateProcessor->saveAs('test1.docx');
Tested it works!

Why am I getting "Call to undefined function get_filenames()"?

I'm trying to get a list of file names using the url helper but I'm getting the following error:
Call to undefined function get_filenames()
I auto-loaded the url helper and then explicitly loaded it in the Controller and I still get the error. Why am I getting this error?
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Downloads extends CI_Controller {
public function index()
{
$this->load->helper("url");
$this->template->write_view('content', 'download_view');
$this->template->render();
}
}
View:
<? get_filenames("/somefolder"); ?>
You have loaded the url helper which does not contain anything similar to get_filenames().
Maybe you meant to load the file helper instead:
$this->load->helper('file');
You don't have a function named get_filenames() defined. May be need to include the file correctly. Trying using $this with the name.

Categories