I have been working in Joomla 4.2.5 and I need to convert my custom components from Joomla 3 to Joomla 4 compatible. I am trying to call model from my custom component into Joomla user controller file, but I am getting an error and code does not perform execution.
I have tried few code but it is not working.
Joomla 3 code for call model from custom component into user controller:
UserController.php
JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_mycomponent/models', 'MycomponentModel');
$model = JModelLegacy::getInstance('Mycomponent', 'MycomponentModel');
Here is my model file code in
components/Mycomponent/Models/mymodel.php
class MycomponentModelmycomponent extends JModelLegacy
{
public function somefunction(){
}
}
Below is my attempt to convert above code into Joomla 4:
UserController.php
Use Joomla\CMS\MVC\Model\BaseDatabaseModel;
class UserController extends BaseController
{
public function login()
{
login functions....
BaseDatabaseModel::addIncludePath(JPATH_SITE .'/components/com_mycomponent/models');
$model = BaseDatabaseModel::getInstance('Mycomponent', 'MycomponentModel');
$model->myfunction($argument);
}
}
components/Mycomponent/Models/mymodel.php
namespace Joomla\Component\mycomponent\Site\Model;
use Joomla\Registry\Registry;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\ListModel;
class IbousersModelIbousers extends ListModel
{
public function myfunction($argument){
}
}
But it is not working, can anyone suggest me what is wrong or how can I call model from another component in Joomla 4?
In Joomla4, you get the model from the MVCFactory.
So the equivalent of:
BaseDatabaseModel::addIncludePath(JPATH_SITE .'/components/com_mycomponent/models');
$model = BaseDatabaseModel::getInstance('Mycomponent', 'MycomponentModel');
Would be:
$mvc = Factory::getApplication()->bootComponent('com_mycomponent')->getMVCFactory();
$model = $mvc->createModel('MycomponentModel', 'Site');
Related
I'm using Codeigniter v4 and wanted to show a custom view to users, so I made a Controller named Test:
<?php
namespace App\Controllers;
class Test extends BaseController
{
public function index()
{
$this->load>model('Usermodel');
$data['users'] = $this->Usermodel->getusers();
return view('custom');
}
}
And a Model named Usermodel:
<?php
namespace App\Models;
class Usermodel extends CI_Model
{
public function getusers()
{
return [
['firstmame'=>'Mohd','lastname'=>'Saif'],
['firstname'=>'Syed','lastname'=>'Mujahid'],
['firstname'=>'Mohd','lastname'=>'Armaan']
];
}
}
And the view custom.php already exists in the Views folder.
But when I load the url http://localhost/ci4/public/index.php/test I get 404 Not Found error message.
Also I tried http://localhost/ci4/public/index.php/test/index but shows the same message.
So how to load this method from the custom controller class in Codeigniter v4 properly?
Except you're not parsing the $data to the view (you should do that adding a second parameter to view('custom', $data)), the code doesn't seem to be the problem: When a view could not be loaded in CI, it shows a specific error message (CodeIgniter\View\Exceptions\ViewException), not a 404 Not Found message.
Probably the problem is in another part of your project.
Try this
<?php
namespace App\Controllers;
class Test extends BaseController
{
public function index()
{
$this->load>model('Usermodel');
$data['users'] = $this->Usermodel->getusers();
return $this->load->view('custom',$data);
}
}
add the $data array to your views
I'm using the latest 'master' branch of CodeIgniter 4
I have a Library that I'm trying to load automatically. Effectively, I want to have have 'one' index.php (that has meta, the basic html structure, etc) through which I can load views via my 'Template' Library.
My Library file: (~/app/Libraries/Template.php)
//class Template extends CI_Controller
class Template {
/* This throws an error, but I will open up a separte thread for this
public function __construct() {
parent::__construct();
}
*/
public function render($view, $data = array()) {
$data['content_view'] = $view;
return view('layout/index', $data);
}
}
I also have a controller set up:
class Locations extends BaseController
{
public function index()
{
return $this->template->render("locations/index", $view_data);
//return view('locations/index');
}
//--------------------------------------------------------------------
}
In ~/app/Config/ I added my Library
$classmap = [
'Template' => APPPATH .'/Libraries/Template.php'
];
I'm getting the following error:
Call to a member function render() on null
What am I doing wrong that's causing my library not to load?
In CI4 the BaseController is where you create things that you want to be used by multiple other controllers. Creating classes that extend others is so very easy in CI4.
It seems to me that the only thing you are missing is creating the Template class. (There are a couple of other minor things too, but who am I to point fingers?)
One big item that might be just that you don't show it even though you are doing it. That is using namespace and use directives. They are must-do items for CI 4.
Because of where you have put your files you don't need and should remove the following. See how I've used use which imports namespace already known to the autoloader.
$classmap = [
'Template' => APPPATH .'/Libraries/Template.php'
];
First, the BaseController
/app/Controllers/BaseController.php
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Libraries\Template;
class BaseController extends Controller
{
/**
* An array of helpers to be loaded automatically upon
* class instantiation. These helpers will be available
* to all other controllers that extend BaseController.
*
* #var array
*/
protected $helpers = [];
protected $template;
/**
* Constructor.
*/
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);
$this->template = new Template();
}
}
/app/Controllers/Locations.php
class Locations extends BaseController
{
public function index()
{
// set $viewData somehow
$viewData['someVar'] = "By Magic!";
return $this->template->render("locations/index", $viewData);
}
}
/app/Libraries/Template.php
<?php namespace App\Libraries;
class Template
{
public function render($view, $data = [])
{
return view($view, $data);
}
}
/app/Views/locations/index.php
This works as if... <strong><?= $someVar; ?></strong>
I know I haven't created exactly what you want to do. But the above should get you where you want to go. I hope so anyway.
It's tricky at first.
But I managed to run it successfully
Make sure you give it proper namespace
And then just "use" in your controller Location.
I dont change anything on Autoload.php.
app/Libraries/Template.php
<?php
namespace App\Libraries;
class Template {
public static function render($param) {
return 'Hello '.ucwords($param);
}
}
The proper way to call is put use App\Libraries\Template just before class Location extends BaseController
app/Controllers/Locations.php
<?php
namespace App\Controllers;
use App\Libraries\Template;
class Locations extends BaseController {
public function index() {
$template = new Template();
$renderedStuff = $template->render('World!');
echo $renderedStuff;
}
}
How does this work?
Notice in Template.php there is a namespace namespace App\Libraries;, so CI4 will automatically load that library properly also recognize the "Template" class. That is proper way to create CI4 libraries in my point of view.
How do we use that library?
Look at my example of Locations.php and then see this code use App\Libraries\Template;, that's how we call that libraries.
How do we call the function?
Look inside the index() function, here we call class Template using var $template = new Template();.
Then we call render() function in Template library with $template->render('World!');.
Just as simple as that.
I hope thats help, lemme know if it doesnt works. :)
Just a little hint, as my eye was hooked by the CI_Controller part.
You seems to use CI3 syntax within CI4, at least about loading the view, which translates to just:
$data = [
'title' => 'Some title',
];
echo view('news_template', $data);
See the CI3doc vs the CI4doc , the "static page" tutorial.
I have a base controller as follows
<?php
use Phalcon\Mvc\Controller;
class ControllerBase extends Controller {
public function initialize() {
}
// wrapper function for debug purposes.
public function pr($data = null) {
echo '<pre>';
print_r($data);
echo '</pre>';
}
}
and a users controller as follows
<?php
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\View;
class UsersController extends ControllerBase {
public function initialize() {
// initialize parent, here ControllerBase.
parent::initialize();
}
public function loginAction() {
// disable the main layout.
$this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);
// disable the controller layout.
$this->view->disableLevel(View::LEVEL_LAYOUT);
}
.
.
.
.
other functions...
}
i was wondering if i could call all the required phalcon classes in base controller and extend then to all the child classes so that i dont need to call them individually on each controller.
in otherwords, can i add the below code
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\View;
only in the base controller and acces them in other controllers. I tried putting them base controller but it gave error : Class not found.
Is this the right way or is there something wrong in my approach...please help.
If I understand your question correctly the answer is NO.
Namespaces are language feature and works this way. The use Phalcon\Mvc\Model\Criteria only declares that you'll use Criteria class from Phalcon\Mvc\Model\ namespace. So in your code you can write new Criteria() to create object instead of using its' full name new \Phalcon\Mvc\Model\Criteria().
You must declare each class in every file which instantiates object of that class so autoloader will know in which file given class exists.
I am trying to run a sample example using Laravel 4 and i am getting errors like, class not found. Can someone help me out here?
controller
file name : authors.php
<?php
class Authors extends BaseController {
public $restful = true;
public function get_index() {
return View::make('authors.index');
}
}
?>
routes.php
Route::get('authors', array('uses'=>'authors#index'));
Views/authors/index.php
<h1> First program in Laravel 4 </h1>
Fitst of all your authors!=Authors, so make sure of your conyroller name in the Route.
And if you want RESTful controller then you can define your route like Route::controller('baseURI','ControllerName'),
Laravel allows you to easily define a single route to handle every action in a controller using simple, REST naming conventions. First, define the route using the Route::controller method..
To know more check restful-controllers
In your example you have to rename your get_index method to getIndex as L4 is camelCase
//AuthorsController.php
class AuthorsController extends BaseController {
public $restful = true;
public function getIndex() {
return View::make('authors.index');
}
}
//route.php
Route::controller('authors', 'AuthorsController');
I'm trying to populate the select box with countries' timezones. I have seen examples and answers here, but nothing works out for me. I am working on Cakephp 2.3.
My timeZone helper class is in this directory
App/View/Helper/TimeZoneHelper.php.
Here is my controller:
class TimeZoneController extends AppController{
public function index(){
$helpers = array('TimeZoneHelper');
}
}
my view
<?php
echo $timezone->select('timezone');
?>
It isn't working, and I don't know how it works because I have never used this functionality before.
You are working with cake2.x. But you are using 1.x syntax.
The correct syntax for 2.x is:
echo $this->Timezone->select('timezone');
(instead of $timezone-select)
Its also in the documentation by the way:
http://book.cakephp.org/2.0/en/views/helpers.html#using-helpers
When you want to add helper in action you need to use following code
class TimeZoneController extends AppController{
public function index(){
$this->helpers[] = 'TimeZoneHelper';
}
}
If you want the helper to available to all the action
class TimeZoneController extends AppController{
public $helpers = array('TimeZoneHelper');
public function index(){
}
}
If you want the helper to be available to all controllers then you need to add it to /app/Controller/AppController.php
class AppController extends Controller{
public $helpers = array('TimeZoneHelper');
}