Unable to load controllers from another controller - php

I'm trying to load a controller inside another controller.
$data['com_top_menu'] = $this->load->controller('account/com_top_menu');
However, this seems to not work when I'm trying to load a controller that is located in the same folder as the controller I'm loading it from.
Tried loading controllers from other folders and seem to not load as well. It seams to load only from the 'common' controllers folder.
Edit:
Actually it seems that the controller is loading. If I place an echo in the middle of the loaded controller it will show the output before the template rendered. So, it looks like the controller is loaded and just doesn't output anything through the rendered view, unless it is a controller inside the common folder.
Files are all in place, controller loads, it just doesn't output anything through the view.

Few things for to load controllers-
1st - you can only load controller from same folders (admin/ catalog).
2nd - you can load controller from any subfolder, just need to pass correct loading path.
3rd - If Opencart hasn't that file than it will not display any error, result will be null/ false.
4th - If you are defining any function name then it will call that function else will call index function so in your case index.
5th - Please use this
return $this->load->view('your.tpl', $data);
Instead of
$this->response->setOutput($this->load->view('your.tpl', $data));
6th - Please enable your debug mode from php/ admin so that you will know any error if your code is throwing. Clear your error.log and then try to load controller.
7th - If these all points are code is not working then do 1 thing - add a blank controller with index function and just add one line so that you can return it's result from view then just
echo 'here';
In your view. If OC is not returning this result it's mean you have error in Opencart files else there is error in your code.
You can say these are same in a way (i am not saying completely and don't want to hurt anyone feelings ;)) but this code
$this->load->controller('account/com_top_menu');
is equal to (based on your autoloader)
$obj = new ComTopMenu; //assuming your class name
$data['com_top_menu'] = $obj->index();
so for your solution please check
- you have file com_top_menu.php in your catalog > controller > account >
- your file class name must be ControllerAccountComTopMenu (or any uppercase or lowercase combination but without _)
- your class must have index function because in your case it calling index.

Related

Laravel #extends() pointer system

Laravel 5.7.
If I navigate to a page that does not exist, I get 404 error page handling.
That view is located in vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Views/404.blade.php
However this file extends:
#extends('errors::illustrated-layout')
This is located in the same folder, and is named illustrated-layout.blade.php
So I guess that the errors:: part points to the specific folder., e.g. vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Views/
Question: Is this type of pointer something that could be created manually, so a person wouldn't have to write the entire path to a specific folder, when extending a view? Would make things much more clean.
You can add a view namespace and achieve the same result.
For example, you can add the following in AppServiceProvider#boot:
$this->app['view']->addNamespace('admin', base_path() . '/resources/views/admin');
and let's suppose you have a blade file in resources/views/admin/layouts/master.blade.php
you can access it with admin::layouts.master

Codeigniter HMVC: Second child module not working

Please refer this question Codeigniter: Error in loading HMVC sub module model
Now I am setting up second level of module like
mysite.com/admin/hr/positions/
Directory structure is this
modules/admin/
modules/admin/models
modules/admin/controllers
modules/admin/views
modules/admin/models/dashboard/
modules/admin/controllers/dashboard/
modules/admin/views/dashboard/
modules/admin/models/hr/
modules/admin/controllers/hr/
modules/admin/views/hr/
modules/admin/models/hr/positions
modules/admin/controllers/hr/positions
modules/admin/views/hr/positions
Now I have respective MCV for every directory. Example Admin, Dashboard, HR and Positions. The system works fine till HR so if I enter site.com/admin/hr/ it is giving output
but when I try to access positions page site.com/admin/hr/positions/ it is giving me 404 no idea what is wrong in it?
Is it because that module is under HR ?
I am loading view $this->load->view('admin/hr/positions/index', $this->data); this way.
The controller name is part of the path as well.
To access function index() of class Positions in /admin/hr/positions/ you need to call /admin/hr/positions/positions/.
To avoid that, move the Controller file positions.php to /hr/.
The path structure of CodeIgniter is always /path/to/controller/controller_name/function_name/as/many/parameters/as/you/want.

OpenCart - 404 page not found error with new module

OpenCart 1.5.6; Theme: Default.
I've created a new module 'Seller' cloned by 'Manufacturer' ... Admin side & Front side is working okay, means adding / editing / saving data is working okay, except an issue.
When I click on 'Seller' link at product page (frontend), it shows 404 error / page not found, ideally it should work same as Manufacturer module works, should open seller's page with list of products by the same seller.
What could be the reason? Because I don't see any error in log files / VQMOD, it just shows 404 error.
Any clue?
Probably this happened here:
Let's assume Your seller URL is http://my.domain.com/index.php?route=product/seller&seller_id=1.
Now the route part product/seller tries to load this controller file:
catalog/controller/product/seller.php
^^^^^^^^^^^^^^
and while there is no action specified (which would be e.g. product/seller/showList) the index action is called. This all means, that You need to have the above mentioned file, which has to contain a class ControllerProductSeller extending from Controller and this class has to have method index implemented.
This would look like
class ControllerProductSeller extends Controller
{
public function index()
{
// ...
}
// ...
}
After this is fulfilled You should not receive a 404 error.
I can see You are completely new to OpenCart and new modules creation (reminds me of my starts) and my advice is: look and discover how the things in OpenCart are done, copy+paste+rename wisely. Most of errors like this (and missing template, language, model files, undefined method names etc) are caused by improper renaming, or in other words, by hot head and quick fingers... Slow down and start thinking a little bit about what are You doing and what needs to be done.

Working with CodeIgniter routes?

I think this is a route issue but I'm not sure. I have a page with this URL:
siteurl.com/kowmanger/titles/titles/edit/$id
I'm trying to find out that when I'm on this page I load the titles page it says page not found so I need to tell it that the $id is just a paramter so I can use it to get the data of the title.
UPDATE :
So I decided to change my titles controller so that there's a edit and add function inside of the titles controller that way they dont' have separate controllers when they are in fact methods.
So now I have:
kansasoutalwwrestling.com/kowmanager/titles/titles - list of titles
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
kansasoutalwwrestling.com/kowmanager/titles/titles/edit/$id - edit form
I don't have any routes set up so far for this. For some reason though I"m getting the same page for both of these page.
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
(right link url) kansasoutalwwrestling.com/kowmanager/titles/add -
addnew form
I need a route so that it'll show the correct url if the add method is accessed.
Also I need to set up a route so that if the correct edit link is accessed it sees the id attached to the end of the url and it'll accept it so that I can do a my database query to get the title data.
UPDATE: So to reiterate I have a module(subfolder) called titles. Inside of the module I have a controller called titles and inside of that controller I have 3 functions called index(), add(), edit().
I tried using Chris's suggestion on the routes but its not routing correctly. Also wanted to mention I'm using wiredesignz modular separation framework if that matters.
Any additional ideas?
Possible answer based on your post, not one hundred percent your entire structure but if i had to guess based off the post I would try this as my routes first..
$route['titles/titles/edit/(:any)'] = 'titles/titles/edit/$1';
$route['titles/titles/add'] = 'titles/titles/add';
$route['titles/titles'] = 'titles/titles';
$route['titles'] = 'titles/index';
Are you using custom routing in your configuration files ?
The general routing protocol used by codeigniter is like this:
domain.com/controller/methode/param1/param2/param3
This being said, your url
siteurl.com/kowmanger/titles/titles/edit/$id
corresponds to something like this :
class Kownmanger extends CI_Controller
{
public function titles($titles, $action, $id)
{
}
}
In case you are using sub-folders in your controllers folder, what I have just said will change, Could you please tell us what's your directory structure ?

Making a controller for home page

Can you tell me how to use a controller for home page because i'm trying to put a model's data in home.ctp (homepage view) with
<?php $this->user->find() ?>but it returns
Notice (8): Undefined property:
View::$user [APP\views\pages\home.ctp,
line 1]
You should check out the cookbook; it has some solid CakePHP tutorials, at http://book.cakephp.org/
You haven't really provided alot of information, but my guess is your Controller uses a model 'User', and you're putting $this->user->find() in your view, when it should be in your controller. In your controller's action you'll want/need to do something like this...
Users_Controller extends AppController {
function index() {
$arrayOfUsers = $this->User->find(...);
$this->set('users', $arrayOfUsers);
}
}
You can then - in your View - access 'users' like so:
pre($users);
... since you used the Controller method set() to send a variable $users to the view.
All you really need to do is create a new controller if that's the direction you want to go. If this is the only statement you have that requires data access, it might be worth faking it in only this method of the PagesController. For example, one of my projects' homepages is 99% static save for a list of featured events. Rather than move everything out to a new controller or even loading my Event model for the entire PagesController (where it's not needed), I just applied this solution in PagesController::home():
$attractions = ClassRegistry::init ( 'Attraction' )->featured ( 10 );
Works great. If your page is more dynamic than mine, though, it may well be worth routing your homepage through a different controller (one that is more closely related to the data being displayed).
The default controller for the home page i.e home.ctp view, is located in /cake/libs/controller/pages_controller.php. This controller can be overriden by placing a copy in the controllers directory of your application as /app/controllers/pages_controller.php. You can then modify that controller as deem fit i.e. use models, inject variables to be used in the home page etc

Categories