CodeIgniter - Loading View inside subfolder with Id - php

I'm working on a new project and I'm using subfolders with my Views and Controllers because it's going to be a fairly big application. My problem is I can not load a View with an Id. I'm getting "Unable to load requested file" error.
This will load my view, but because I don't have the Id needed in the URL I'm getting errors because some of the functions I have require it:
$this->load->view('administration/header');
$this->load->view('administration/add_vendor_location');
$this->load->view('administration/footer');
But when I try and include the Id like so:
$this->load->view('administration/header');
$this->load->view('administration/add_vendor_location/Id');
$this->load->view('administration/footer');
I'm getting the "Unable to load the requested file" error. I know I can use re-direct, but this is coming from a Form Validation and setting all the POST data and errors in sessions is very time consuming / annoying.
Hope this makes sense!
Thanks!

In the code you provided, views/administration/add_vendor_location/Id.php must exist in order to load it.
If you're trying to pass a variable to the view, you would do so using an array in the secend parameter:
$this->load->view('administration/add_vendor_location', array(
'id' => $id
));
See the CI View documentation

Related

TYPO3 - How to make a get request to a controller action

I have an application in TYPO3 CMS. It has an extension test_extension that has a controller and an action. This action should return some JSON.
class TestRequestController extends ActionController
{
public function testAction(): void
{
echo json_encode([
'test' => 123
]);
}
}
I want to be able to request this action via Postman. How can I do that? TYPO3 version - 8.7. Thanks in advance!
Creating Links
Usually extbase-extensions are created with the help of the extension extension_builder. This extension creates by default templates and links to open list- and detail-view.
It's possible to add additional actions and to create according links.
Also the usage of the templates is not required and your way to return the result of the action without usage of a template is possible.
The logic of the links is this, I break the parts down in single lines:
tx_extension_plugin[controller]=TestRequest
tx_extension_plugin[action]=test
There are still more parameters commonly used like id or search, but it's also possible to define individual parameters.
In your case the listed parameters seem to be sufficient and the link would look like this now:
www.example.com/?id=123&tx_extension_plugin[controller]=TestRequest&tx_extension_plugin[action]=test
for the extension news this would look like this, this is with your parameter-values which are not available in news. This example shows only how the extension-related part is handled (tx_news_pi1):
www.example.com/?id=123&tx_news_pi1[controller]=TestRequest&tx_news_pi1[action]=test
id is for the page here and not a parameter for your extension, else id had to look like this tx_extension_plugin[id]=123. So all extension related parameters have the form tx_extension_plugin[parameter]=value.
While it's possible to create those links with the API, it's easier to create them with the view helpers for the fluid templates. Note that sometimes an hash is added at the end, like this example: &cHash=1234567890.
The cHash-value you can't create without viewHelper or API, so the knowledge about the parameters and the other values is not enough to create the links.
Calling the link
Most often links are directly called by the browser and visible in the URL-bar. But sometimes and in your case you might call the links by AJAX, so that the json is loaded without being directly shown to the user.
It's also possible to wrap the json in script-tags, so that it's every time loaded when the whole page is called, it's not dynamic then and without AJAX it can't adjust to some user-interaction without loading a whole page again.
AJAX responses can be realized in many ways in TYPO3, the most easy one is to define a special page-type and a special page in the pagetree for it. On this page you add the plugin of your extension to return the json. This "Ajax-page" has to be configured to have the correct header for Json and must not return anything else but the JSON, so all HTML-Output has to be disabled.

DOMDocument->loadHTMLFile() I/O warning : failed to load external entity, only fails when called by ajax

I have a function which creates a new instance of a form template I've made (not using a template engine) and fills it in with various data based on an object thats passed to it. I use this function to create forms for existing objects when the page is loaded, but I also have a button in the interface which makes an ajax request to a separate script which creates a new object and then calls that function to create a form for the object to be sent as a response to the ajax.
The error is:
Warning: DOMDocument::loadHTMLFile(): I/O warning : failed to load external entity "templates/form_edit_event.html" in C:\wamp64\www\<private>\src\<private>\components\component_edit_event.php on line 12
The code it references:
11 $eventDom = new DOMDocument();
12 $eventDom->loadHTMLFile('templates/form_edit_event.html');
The thing is though, when the page is first loaded, my script and the loadHTMLFile function work just fine to create the forms for the already existing objects. The I/O error only occurs when I use the button to try to make a new one. The exact text of which is as follows, although I obscured some directory names that shouldn't matter:
The files it's loading is pure html, not even a header/footer/etc, just <form>contents</form>. I have no idea why it would fail only sometimes.
The entire script (minus some irrelevent use/includes) being called from ajax is this, with component_edit_event.php being the script mentioned above which creates the form:
include_once __DIR__ . '\..\components\component_edit_event.php';
$tourneyId = $_POST['tourneyId'];
/** #var DimTournament $tourney */
$tourney = DimTournament::findById($tourneyId);
$newEvent = new DimEvent($tourney);
echo createEventForm($newEvent)->saveHTML();
I don't think I should need to include any other code but I absolutely can if needed.
Swapping the relative path in $eventDom->loadHTMLFile('templates/form_edit_event.html'); to an absolute path fixed the issue. The script that was throwing the error was in a different directory from the script which was working fine.
I'll be making a config file with some sort of $srcRoot variable for the sake of keeping some semblance of pseudo-relativity in the path names.

How to add comments to each forum post in yii, using bbii forum module and comments-module

I am using Yii bbii forum module and it works fine. But now I want to add comments-module so every forum post could be seperately commented.
At the begining it might look:
I followed instruction what is here, but I can't make it work :(
And why I even need to include this file, if I want to add just comment?
When I added the same widget to user page (just for testing) - I got "This item cann't be commentable" and it's fine because probably I don't have correct configuration in main.php.
Difference between widget in user model view and forum view is data passed in it.
Here:
public function actionPostComment()
{
if(isset($_POST['Comment']) && Yii::app()->request->isAjaxRequest)
{
$comment = new Comment();
$comment->attributes = $_POST['Comment'];
var_dump($comment);
var_dump returned this when tried to submit comment in forum, and here in user view page.
And probably it is not even possible to combine these to modules? I'm really new in Yii.
Updated:
Basically what I have done is:
exstracted comment module (under protected->modules)
in main.php (under protected->config) added all cofiguration in modules array:
'comments'=>array(
//you may override default config for all connecting models
'defaultModelConfig' => array(
//only registered users can post comments
'registeredOnly' => false,
'useCaptcha' => false,
.......
and in view file _post.php added following:
<?php $this->widget('comments.widgets.ECommentsListWidget', array(
'model' => $data,
));
and var_dump($data) gives this (when this is called in controller where post is reseaved).
An error message was given here:
include(BbiiPost.php): failed to open stream: No such file or directory
You said that the Bbii was working with Yii and it broke when you tried to add comments.
The links to your var_dump files are broken, but I did try to read them ;)
It looks like the comments module is interfering with the POST path so that when the form submission comes in it is in a different path from the root which is confusing the YiiBase's autoloader.
You could try explicitly adding the path to BbiiPost.php to the autoloader's search path, or finding where the include("BbiiPost.php") line is and changing it to an absolute path.
Another possibility is that the forum page you are on has links to add comments but the page routing has not been taken from the route. So it might be that the POST link to the comments is actually at /forum/123/comment/add instead of just /comment/add. So when the form is submitted it is trying to the comments/add controller/action but finding that it is in /forum/view and getting confused about the paths to the include files.
I have generally found that the instructions on the Yii (v1) [v2 docs are much better] site for these modules is flaky at best. Quite often the source download link on the page points to an old buggy version of the code as the project has usually moved somewhere else. You generally need to have a pretty good PHP/Yii knowledge to debug these user-submitted modules and get them working.

How to use editUrl attribute of jQgrid in jQuery

I have been trying to implement edit/add of rows in jqgrid dynamically but stuck at place with editUrl attribute. Can somebody tell me that
1) Where to keep this php file, in server or in our applcation?
2) I am using like editurl: './data_save.php', and I have this file in my application, next to my jsp file. But on submit, I am getting 'error Status: 'Not Found'. Error code: 404' on top of popup window and on console I am getting, no mapping found for request with URI [/myapp/some.php] in distacherservlet with name 'myApp'.
Any idea?
Update :-
I also tried using, editurl:'/some.jsp' and when I create on controller(expecting this url) in my Spring MVC Controller class then I do not get message at console saying, "no mapping found for request with URI [/myapp/some.php] in distacherservlet with name 'myApp'"
Use your application name
editUrl:'WhatEverYourApplicationNameIs/data_save.php'
Update1:
editUrl:'#Url.Action("ActionName","ControllerName")'
Update2:
If your edit method is like below
public .. MyEditMethod()
{
//Add logic
}
then your editUrl will be
editUrl:'#Url.Action("MyEditMethod","WhatEverYourControllecNameIs")'

zend layout.phtml error

Hi I've got an error in my Zend Framework project...
It raised:
Invalid controller specified (myweb)
Here is my apache error.log:
PHP Warning: Invalid argument supplied for foreach() in /var/www/myweb/application/layouts/scripts/layout.phtml on line 107
Here's my code in layout.phtml:
<?php foreach($this->category as $categories):?>
<li><div id="sidemenu"> �<?php echo $categories['categoriesName'];?></div></li>
<?php endforeach;?>
Can anybody help me?
The problem seems to be that for some reason your url is presenting myweb as the controller instead of what should be your controller.
My guess is the you are trying to use localhost to display your application so are presenting a url similar to http://localhost/myweb/...
While it is possible to use the localhost to view ZF applications it often becomes inconvenient as applications become more complex. I would suggest you use a vhost for anything more then a very simple application.
I'm pretty sure that when you resolve the url problem the php warning will likely fix itself.
It seems like the variable $this->category is not set in the Controller. You can do this by defining $this->view->category from your controller.
Mostly when using these controller generated variables in the layout script instead of the corresponding view script, you want to use the same data in every view. If this is the case, check this question: Sending variables to the layout in Zend Framework
This error come because your array $this->category is blank. if this is blank array or return nothing then how foreach loop will execute?
so first print this array and check.

Categories