Loading View Page In Library In Codeigniter - php

In my codeigniter i created a library in library folder.I want to load view pages in that library.How can i do this?
This is my code:
$this->load->view('view_page');
But when iam using this code i get an error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_theme_lib::$load
Filename: libraries/theme_lib.php
Line Number: 9
What is the problem in mycode?
In Line number 9 in library the code is :
$this->load->view('view_page');

To do what you're trying to do you need to get an instance of CI, and use that.
e.g.
$CI =& get_instance();
Within your library functions you could then use this variable to load the view:
$CI->load->view('view_page');
I would question though why you want to call a view, in the form that you have done, within a library. I suspect that you would be better to get the view call to return data (setting the 3rd parameter 'true') and then have your library return the display data to the controller.... Your approach seems messy, but then I have no idea what your library is trying to do.....

I have came across your question for different reason, I seem to have problem passing variables to views instead. Let me explain before I tell you answer to your problem.
Imagine you have an Emailer library to send emails rather than sorting that out in controller.
Emailer than within itself builds email string using views. My problem is that when I make my call from controller something like Emailer::send_mail($data,$template) it passes the variables correctly but when I do it from another library the view fails to register the variables. LOL
So yes Stéphane Bourzeix you do sometimes want to use output from view in a different way than just returning to client browser.
The solution is here.
https://www.codeigniter.com/userguide2/general/views.html
the last section of that page has something like
$string = $this->load->view('myfile', '', true);
but something like
$string = $this->load->view('myfile', $view_data, true);
should work too
in case of doing this from other places than controllers you will need to:
$this->ci = & get_instance();
$string = $this->ci->load->view("myfile",$view_data,true);
it seems like the last argument in the list (true) is the one that tells it not to render to browser but instead just create string with template content
I know it's a bit too late but hope it still helps to some. Good luck with your code.
tomhre

You simply DON'T load pages (aka Views) in a Library.
I don't see any need for doing this.

Related

CodeIgniter, Know the controller which called function

I am trying to create library that will write "error" messages in database, I do not think that I will be able to write PHPs errors nor CodeIgniters, but I can write my own "errors" something like
$this->error->write("User is not allowed to go in here");
I know there is a error handling already built in CodeIgniter on top of all it only supports writing errors/infos/debugs in file not database.
The real question is: how to get controller that called function.
Lets say I am in controller -> ./admin/settings.php and error ocures, code will call my library and I want it to store controller that called it. (I may be forced to send it as parameter but I don't want to write manually that it was fcn("error text", "/settings.php");
Assumed output: /controllers/admin/settings.php somewhere inside class that is called by controller.
You can use the router class to get this info
Also works with Codeigniter v3.0.x
To get the controller(class)
$this->router->class
To get the method(function)
$this->router->method

When I move code into a function, I get a Fatal error. How can I call code from function

There are two pieces to this code:
One that adds documents to an index to be searched, which works fine, and a crawl() function that is a web-crawler that gets the contents of a page, which also works fine.
But, I need to add a document from inside the crawl() function.
When I move the code that adds a document inside the crawl() function, I get a Fatal Error:
Fatal Error: call to member function addDocument() on a non-object.
I am wondering how I can access the member function addDocument() from inside the crawl function?
Right now, I have a working version where the crawl() function returns what it has crawled in the form of a variable and then the addDocument code, outside the crawl() function, also has access to the returned variable and adds the document to the index that way.
But, that only (logically) works when I am crawling one page or a page with no links to follow. As the function only returns when it is done and since it is recursive to follow a page's links, the only content it will return is the content of the last-page.
Where I need the content of each page to be added each as a new document in the index.
Here is the working code, described above, commented as much as I could: http://pastebin.com/5ngcucDp
and here is the non-working code where I try to move the addDocument() inside the crawl() function: http://pastebin.com/mUEwQJTG
If you have a solution that involves how to access the addDocument() function from inside the crawl() function, then please share.
Or if you have a solution that involves modifying the working code so that it returns the contents of each page it crawls instead of the last-page, please share.
If you have any solutions, please share as I am absolutely exhausted and have tried everything I know.
When moving code to a function, you are completely removing its ability to access variables in the same scope. In this case, you probably (not going to go looking through your off-site code) have something like $someObject = new myClass();, then are trying to access $someObject->addDocument() on it from within the function.
You need to pass $someObject as a parameter to the function, or you could use global $someObject inside the function, though it's not as good an idea.
You have specified that:
// The below line is where the error takes place.
$elasticaType->addDocument($document);
Is your error line. Now, PHP is trying to access a class linked to $elasticaType If you have a linked class then use:
$elasticaType = new ClassName();
If not then you should create a class:
class Name {
public function addDocument ($document){
//Add document code
return $somevar;
}
}
$elasticaType = new Name();
$elasticaType->addDocument($document);

Problems using a Wiki to HTML script (FFNN) as Helper in CodeIgniter

I've found this script for converting wiki syntax to HTML in php and i've tried to integrate it into Codeigniter. it seems really easy to use. However, it's not working, and instead producing around 8 of these errors:
Message: Use of undefined constant LS_NONE - assumed 'LS_NONE'
I think this is because Codeigniter helpers are not a class but rather functions, and this bit of code is a class, or does this issue lie with something else? I've also tried to use it as a model without success.
It also seems horribly outdated (2007). Could somebody suggest a really simple alternative or maybe give an idea of how to convert this to a simple function if that is possible? It's a very short piece of code. I'm not sure how these constants work in relationship to a function versus a class.
I've given the Text_Wiki from Pear ago, but the use and complexity well exceeds both my requirements and knowledge :)
//Any help would be greatly appreciated
Loaded using:
$row = $query->row();
$content=$row->course_content;
$this->load->helper('wiki');
$content=explode("\n", $content);
$output = WikiTextToHTML::convertWikiTextToHTML($content);
$html=array_merge($output);
$data['contents'][]= $html;
$this->load->view('default/a',$data);
It looks like the script is actually a class. put it in the libraries folder and load it with $this->load->library(). That will allow it to properly initialize and define the constants that it uses.
something like:
$this->load->library('wikitexttohtml');
$this->wikitexttohtml->convertWikiTextToHTML($wiki_text);

Is there a way to tell Smarty not to print an expression?

I'd like to use Smarty in conjuction with the Zend Framework, especially some of it's View Helpers.
Now i got to the point, where i implemented a Zend_View that uses Smarty to display templates. I can assign values as usual. So far so good.
Now I would really like to use Zend View Helpers in Smarty. I asssigned the Zend_View object as "this" and tried this in the template:
{$this->layout()->setLayout('default')}
As this will print the return value of the setLayout() method (which is a Zend_Layout), there is an error:
Catchable fatal error: Object of class Zend_Layout could not be converted to string in /path/to/templates_c/089c3d67082722c7cabc028fa92a077f8d8b4af5.file.default.tpl.cache.php on line 27
This is clear to me, so I went into Smarty's core to fix this:
The generated code did look like this:
<?php
echo $_smarty_tpl->tpl_vars['this']
->value->layout()
->setLayout('default');
?>
And now it reads:
<?php
$hack = $_smarty_tpl->tpl_vars['this']
->value
->layout()
->setLayout('default');
if( is_string($hack) ||
( is_object($hack) && method_exists($hack, '__toString') ) )
echo $hack;
?>
Now this is probably the worst fix i can think of, for several reasons (Smarty compatibility loss, performance). Sadly, it's the only one. Is there a way to stop Smarty from trying to print the output of the expression? Also, i want the syntax to stay as intuitive as possible, and i don't want to write Smarty functions for all the Helpers, because I want to use this code with a third-party application (Pimcore) that might add new helpers.
Thanks in advance for any suggestions!
Some suggestions (only ideas, nothing so good):
Create a __toString() in Zend_Layout who returns null or empty string (big/ugly/worse workaround).
Create a variable modifier/filter who return null or empty string, so your call will be something like {$this->layout()->setLayout('default')|noreturn} (you can use it with other things too and noreturn can be called with an friendly name like definition or define to indicate the purpose of the instruction, but a workaround too)
Using the assign to build a expression who set all this thing to another var (workaround too).
Maybe this can give you some good ideas =)

CodeIgniter: Can't Get My New Controller/View To Show

I am learning how to use codeIgniter as my php framework. I am reading through the documentation and watching the intro video and just generally following along with the first tutorial, but it's not working for me.
I have created a controller called "test.php" and a view called "test_view". The controller for this class is exactly like "welcome.php" and the view file just has some static html. However, when I go to index.php/test I get a 404 error.
I have also tried manipulating the original welcome files so that instead of calling a view it just echos "testing", yet I still see the original welcome message! I've tried clearing my browsing cash and refreshing, but to no avail.
Any suggestions? Thanks.
Edit: Here's the code for controllers/test.php
<?php
class Test extends Controller {
//Just trying to get it to echo test
public function index()
{
echo "test";
//$this->load->view('test_view');
}
}
?>
Try looking at this page in the documentation - this might solve your problem.
This basically means you should try typing index.php?/test/ instead (notice the question-mark).
First of all, check the above link. Might be useful.
If not, then...
Try changing the default controller in the config file ('routes.php') to something else (probably, to 'test'), then try loading index.php. Just to test whether the whole system works (or not).
Check whether mod_rewrite is loaded (in your server .conf-file, if you're using Apache).
Try using the latest build of the framework. AFAIK, the name of the controller class for now is "CI_Controller".
Finally, try removing the word 'public' before the declaration of the function. AFAIR, CI enable you to make private functions in controllers just by using prefix (which is set in the config file) at the beginning of the name of the function (thus making all the other functions public).
But most certainly the problem is with the mod_rewrite. If not, try debugging with die('Page found'); instead of echo - this will allow you to track possible redirects on the page.

Categories