Where to load View Parser service in Codeigniter 4 - codeigniter

I am not understanding loading the parser service in Codeigniter 4, Please tell me how to load and where to load it, and how can i use it in View.

Load the parser inside of the controller method that you want to use it in e.g. inside of index() method of Home controller. You may load it using $parser = \Config\Services::parser(); or $parser = service('parser');.
Rendering a view can be done by echoing as follows:
echo $parser->setData($data)->render($view);
wherein $data can be something like ['blog_title' => 'My Blog Title', 'blog_heading' => 'My Blog Heading']; and $view is the address of your view file inside of /Views directory.
This will then replace any 'substitutions' in view file such as <h1>{blog_title}</h1> with your data. You can read more about the specifics of substitutions in docs.
Note: Using the Parser, your view templates are processed only by the Parser itself, and not like a conventional view PHP script. PHP code in such a script is ignored by the parser, and only substitutions are performed.

Related

Whats the difference between redirect and this in Codeigniter?

I am new in Codeigniter and it's one of the good frameworks of php. But on some conditions I'm confused. Like this one. If any of you have any clarification about my dough, it's a great help for me.
Offcouse redirects refresh the page and $this not but apart from this I want to know - anyhow both of them used to go to somewhere else on view pages or like in other controller or in same controller to other methods.
But we don't use these side by side because when getting any of them it will go to that page or method without checking the next lines.
In case of a normal difference then have lot's of but I just want to know about the condition of going to next page or method when we use redirect or $this like this -
$this->Function($value); //It's method of same controller.
redirect('Controller/function'); //It's also doing same with page reload.
Thank for looking my problem.
Redirect()
When you will call any function of helper in codeigniter then you can call function directly without using any object. Helper in Codeigniter is collection of functions.
Redirect() method is a part of URL helper in Codeigniter.
For your ref. https://www.codeigniter.com/user_guide/helpers/url_helper.html
So, just load helper using $this->load->helper('url'); or you can also mention in autoload.php file.
$this->Function(); used to call a function from same controller
$this->Function(); used to call a function from same controller
redirect()
While building a web application, we often need to redirect the user from one page to another page. CodeIgniter makes this job easy for us. The redirect() function is used for this purpose.
redirect($uri = '', $method = 'auto', $code = NULL)
The first argument can have two types of URI. We can pass full site URL or URI segments to the controller you want to direct.
The second optional parameter can have any of the three values from auto, location or refresh. The default is auto.
The third optional parameter is only available with location redirects and it allows you to send specific HTTP response code.
Redirect means jumping to another function mentioned in the redirect method.
$this->Function($value); => jumping to another function and you can execute the code of the same function as well as pass the value back by returning value.
When you send request to codeigniter generally CI controller gets called and then function which is mentioned in uri segment. like below... So this will be another request.
redirect('Controller/function'); //It's also doing same with page reload.
But when you have to call another function within the same request then you can use below approach
$this->Function($value); //It's method of same controller.
This will execute the given function and return the value within same request.

Symfony the same variable for all Twig templates

I am writing my own CMS using Symfony 3 right now and i have problem with include the same variables in all controllers.
For example:
I want render logo which i have just upload in my admin panel so i keep URL to it in database and fetch with other data like meta (site descryption, site title etc.) in controller - passing it as array and call in base.html.twig
However base.html.twig extends all other twig templates which i use in controllers so i must fetch it in all of them.
Is there some nice solution for my problem ?
Posting an answer based on your comment.
Use addGlobal() method in your base controller like so:
$this->get('twig')->addGlobal('variable', $variable);
Where $variable is the global variable you need.

How to fetch data from controller to views in CakePHP or Yii

How to fetch data from controller to views in CakePHP or Yii but not embed PHP code in views file (view is html file).
it similar Template Parser Class in Codeigniter:
https://www.codeigniter.com/user_guide/libraries/parser.html
You will need to load the data in the action controller by calling one of the model functions like findAll()
here is an example, assume we have a User model, and you need to pull out all the data and render them at view file:
public function actionIndex(){
$model = Users::model()->findAll();
$this->render('index', array('model' => model));
//the model variable will be defined in the index.php
//file and will have a value as assigned in the array
//index.php file should be located under /views/users/index.php
}
Now in index.php file do a var_dump and see what results you got.
I don't see the problem with embedding php into HTML. But if you really don't want to do it, you can use CakePHP with a template library like Mustache.
Check this: https://github.com/electblake/CakePHP-Mustache-Plugin

Using Cake w/ url extensions

I am using CakePHP 2.0 (I believe it is v2.0.3.) and PHP 5.3.8.
I am working on an application which utilizes Cake's support for url extensions. Specifically, I am outputing XML whenever a url request ends in the .xml extension. If the url request is made without any extension, then my application presents the stardard view. This all works beautifully --
request .../controller/action.xml renders via view/controller/xml/action.ctp while request .../controller/action renders via view/controller/action.ctp.
To achieve this, I did the following:
1. Added support for url extension; added the following line to route.php -- Router::parseExtensions('xml');
2. Added support request handling; added the following line to MyController.php -- public $components = array('Session', 'RequestHandler');
To output xml, I am using Cake's 'XML' class in conjunction with PHP's 'SimpleXMLElement' class. My problem is that the complete xml is never generated. The classes are suppose to generate xml based on an input PHP array, however, it appears that the complete array is not processed. My xml output is partial.
My source code in my view file (.ctp) is as follows:
$simple_xml_elem = Xml::build($xml_array);
echo $simple_xml_elem->asXML();
Interestingly, in the course of trying to debug this problem, I discovered the a similar behaviour can be observed if I simple attempt to dump the view object within the xml view file (../view/controller/xml/action.ctp). 'var_dump($this)' only output a partial dump of the view. The same view dump performed within the standard view file (../view/controller/action.ctp) outputs a full dump of the view.
It is my believe that Cake is somehow setting up the view environment differently when it routes for a url extension than when the standard view is requested.
Could some please shed some light on the for me before I lose my hair. Please???? Thanks!
Have you looked at the response & request objects
http://mark-story.com/posts/view/the-cakerequest-object-in-cakephp-2-0 - the article was written when cake 2 was alpha I think but Mark talks about the concept of request object (another entry for response)
both objects are passed to the controller
http://book.cakephp.org/2.0/en/controllers/request-response.html
for now I believe you use the request comp to handle layout switching (although it can be done without it)

How can I prevent duplicate content when re-routing pages in CodeIgniter?

Say I have a controller, "Articles" but I want it to appear as a sub-folder (e.g. "blog/articles"), I can add a route like this:
$route['blog/articles'] = 'articles';
$route['blog/articles/(:any)'] = 'articles/$1';
This works fine, the only problem now is that example.com/articles and example.com/blog/articles both use the Articles controller and thus resolve to the same content. Is there a way to prevent this?
To add a little more clarity in case people aren't understanding:
In this example, I don't have a 'blog' controller, but I want 'articles' etc to appear to be in that subfolder (it's an organization thing).
I could have a blog controller with an 'articles' function, but I'm likely to have a bunch of 'subcontrollers' and want to separate the functionality (otherwise I could end up with 30+ functions for separate entities in the blog controller).
I want example.com/articles to return a 404 since that is not the correct URL, example.com/blog/articles is.
Shove this in your controller:
function __construct()
{
parent::Controller();
$this->uri->uri_segment(1) == 'blog' OR show_404();
}
You can use subfolders in Codeigniter controllers, so in CI, the following directory structure works:
application/controllers/blog/articles.php and is then accessed at
http://example.com/blog/articles/*.
If, for some reason, you're set on routing instead of accessing the controllers in folders (you want to have a blog controller, for example, and don't want to route to it), you can do as suggested above and add the test for 'blog' to the constructor.
If you're in PHP5, you can use the constructor function like this:
function __construct()
{
parent::Controller();
$this->uri->uri_segment(1) == 'blog' OR redirect('/blog/articles');
}
or, in PHP4, like this:
function Articles()
{
parent::Controller();
$this->uri->uri_segment(1) == 'blog' OR redirect('/blog/articles');
}
I would suggest using redirect('blog/articles') instead of show_404(), though, so that you're directing users who hit /articles to the correct location, instead of just showing them a 404 page.
Routing there does not mean it will use a different controller, it just creates alias url segment to same controller. The way will be to create another controller if you are looking to use a different controller for those url segments.
If both /blog/ and /articles/ use the same controller, you can reroute one of them to a different one by just adding a new rule in your routes file.

Resources