joomla controllers how they work? - model-view-controller

I am struggling to understand how to call sub controllers from a joomla component. What are to be placed in the controllers folder?
I have the entry point of my component like -
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// require helper file
JLoader::register('TieraerzteHelper', dirname(__FILE__) . DS . 'helpers' . DS . 'my_helper.php');
// import joomla controller library
jimport('joomla.application.component.controller');
$controller = JController::getInstance('MyController');
// Get the task
$jinput = JFactory::getApplication()->input;
$task = $jinput->get('task', "", 'STR' );
// Perform the Request task
$controller->execute($task);
// Redirect if set by the controller
$controller->redirect();
Then if I want to call a controller, which is placed in the controllers folder, how do I do that?

You do a task=controller.function
As an example: You want to call the MycomponentControllerFoo in /controllers/foo.php and execute the function bar(). You use the following URL to call this:
index.php?option=com_mycomponent&task=foo.bar
Or you can use a form where there is a hidden task field.

Related

Unable to load the requested file: helpers/common_help_helper.php

it cant seem to load the common helper that i created in the helper folder
to load the admintle for my codeigniter project.
enter code here
<?php
function public_url($url){
return base_url('public/'.$url)
}
You are calling another helper that is being used in your helper.
So you need to load the url helper in your controller before you load yours.
i.e.
$this->load->helper('url'); // Core URL helper for base_url()
$this->load->helper('common_help'); // This is your common_help_helper.php
And you have a missing ; in your function
function public_url($url) {
return base_url('public/' . $url); // <<< Missing ; in your code
}

Why Codeigniter3.0's url can't be blank after doamin name?

I'm corrently develop webs application for selling products online by using Codeigniter and upgrade from Ci2.2.1 to CI3.0.
However I've meet an errors 404 Page Not Found when I enter domain name on url not yet called any controller by hoped default_controller will load instead of type my main controller.
Notes: It is working on Ci2.2.1 and it stop work when upgrade to CI3.0.
It is working if I type any characters or any Controller after domain name(http://localhost/Ecom3/sometroller) But it is coudn't call default_controller and keep empty or blank after domain (http://localhost/Ecom3/).
Errors log: ERROR - 2015-04-24 03:48:57 --> 404 Page Not Found: /index
Please check my webs structure below:
Main->Main_controller->MY_controller->CI_Controller
My purpose to make it easy to controller the templates.
In MY_controller I've use __autoload() function
function __autoload($class){
if (strpos($class, 'CI_') !== 0)
{
if (file_exists($file = APPPATH . 'libraries/' . $class .'.php'))
{
include_once $file;
}
}
}
And I have confige route as below
$route['default_controller'] = "main/Main";
$route['(:any)'] = 'main/main/index/$1';
$route['c'] = 'cat/cat/index/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
The problem:
My url can't be blank after I type domain or after forword slash as below image
Please help
I try your code and get 404 when Main.php in your controller place inside controller/main folder. but when im move Main.php into controller folder, default controller to Main has working. then im try to debuging and see:
// Is the method being specified?
if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
{
$method = 'index';
}
in core/Router.php
thats mean codeigniter3 are not allowed you to set default_controller in sub folder. you must put default_controller in app/controller/ to make it works.
in config/router.php
change this:
$route['default_controller'] = "main/Main";
TO:
$route['default_controller'] = "Main";
then move your controller file :
app/controller/main/Main.php
TO
app/controller/Main.php

How to display a view in Joomla controller?

I have a script that uploads to a folder and I want to display a view after the upload is done, successful or not.
/component
/admin/
/views/
/components/
view.html.php
/tmpl/
default.php
modal.php // I want to load and output this file from controller.php
component.php
controller.php
/site/
I would like to do something like this, in my controller
$view = $this->getView(/* params for modal.php to load*/); //get the view
$this->view->display();
thank you
Try using:
$view = $this->getView( 'components', 'html' );
$view->setLayout('modal');
$view->display();
or
JRequest::setVar('view', 'components');
JRequest::setVar('layout', 'modal');

Where is Itemid used to flag active menu item in Joomla 2.5.6?

Can anyone please help me with the process flow through which the Joomla Itemid parameter ends up being the highlighted menu item?
I have embedded a 3rd party application in Joomla and by temporarily changing the php environment within the application.
I am able to get joomla html and insert the 3rd party html by replacing a token.
Simplified Code:
if ($_SERVER['REQUEST_METHOD'] == 'GET' ) {
$_SERVER['REQUEST_METHOD'] = '';
}
$_SERVER['REQUEST_URI'] = '/joomla/index.php?view=mycom&option=com_mycom&Itemid=103';
$_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'] = '/joomla/index.php';
$_SERVER['QUERY_STRING'] = 'view=mycom&option=com_mycom&Itemid=103';
ob_start();
require_once '/joomla/index.php';
$joomlaHTML = ob_get_clean();
echo str_replace($replacementToken, $thirdPartyHTML, $joomlaHTML);
In v1.5.x, the menu item with ID 103 is properly highlighted but in v2.5.6, it isn't and the Home item is always highlighted. I think it used to be highlighted correctly in v1.6.x and earlier versions of 2.5.x as well but not sure.
I wanted to find how the process flows (where this is set) so I can see what tweaks I need to make.
DELETED WRONG INFO
Thanks Dayo! you saved my day with this:
// force highlight the external url menu item
$Itemid = JRequest::getVar('Itemid');
$menu = JSite::getMenu();
$menu->setActive($Itemid);
I don't fully understand the breadcrumb part, but I managed to get it working by editing my component's controller.php to read:
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// force highlight the external url menu item
$Itemid = JRequest::getVar('Itemid');
$menu = JSite::getMenu();
$menu->setActive($Itemid);
// force fix the breadcrumb
$app = JFactory::getApplication('site');
$pathway =& $app->getPathway();
$bcrumbs = &JPathway::getInstance('site');
// import Joomla controller library
jimport('joomla.application.component.controller');
/**
* MyCom Component Controller
*/
class MyComController extends JController
{
}
Look in the following File
Check the
/modules/mod_menu/mod_menu.php
File and you will see two functions has been called "getActive" and "getDefault"
Which can be find in following file
/libraries/joomla/application/menu.php
I think it can be customized easily now

Autoload Config for Pagination in CodeIgniter not working

I am trying to implement pagination in my CI webapp. Now I put the config for pagination inside a config file like this...
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['base_url'] = "http://example.com/index.php/home/index";
$config['num_links'] = "9";
$config['per_page'] = "20";
$config['total_rows'] = "200";
/* End of file pagination.php */
/* Location: ./system/application/config/pagination.php */
In my controller, I have loaded the library
$this->load->library("pagination");
And I have defined the pagination config file to be autoload in config/autoload.php
$autoload['config'] = array('pagination');
At last I called the method to create links in my view template:
<?php echo $this->pagination->create_links(); ?>
This did not create any links. The configuration is being autoloaded correctly. I checked using...
<?php echo $this->config->item("num_links"); ?> <!-- this dislayed 9 -->
What am I missing here? Just for the record, putting the config inside the controller didn't work either.
Update #1- I have found out that the config settings are loading correctly but they are not reaching the library or something like that. Inside the pagination library - I did some hard coding to find out that per_page parameter was zero in there.
Update #2- I was mistaken when I said that putting the config inline wasn't working. It is working fine. The autoload isn't working.
Regards
Finally used this code to solve my problem...
$this->config->load("pagination");
$page_limit = $this->config->item("per_page");
$config['total_rows'] = $var; // Some variable count
$this->pagination->initialize($config);
This lets me define the config items in a file as well as initialize the items I want in controller like in my case, the total no. of rows - retrieved from database.
Regards
Your autoload line in your config file should be this
$autoload['libraries'] = array('pagination');
And you must have this line in you controller after your config array, before you use create_links() etc.
$this->pagination->initialize($config);

Resources