Joomla 3 - Custom Component - How to access site model from Admin View? - php

I used component creator to generate a custom component for Joomla 3. I have a view in the Administrator panel that would require a function from a model in the front end.
I have been doing google searches for several days trying to locate an appropriate answer, this is the closest I have come to a working response:
How can I include multiple models in one view for in a Joomla 3.x component built with Component Creator
However, in that response he seems to be using a site view model from another site view.
Here is a little bit about my component structure:
name: com_stargazer
Admin View: email
index.php?option=com_stargazer&view=email&layout=test
/administrator/components/com_stargazer/views/email/tmpl/test.php
Site View and model: returnpage
/components/com_stargazer/models/returnpage.php
/components/com_stargazer/views/returnpage/tmpl/default.php
I tried to modify my admin view to include the site model by including the path:
$this->setModel(getModel(JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_stargazer/models', 'returnpageModel')));
$this->setModel(JModelLegacy::getInstance('returnpage', 'stargazerModel'));
// assigns array from the second model to 'ItemsOtherModel.' there is no '$' sign used.
$this->ItemsOtherModel = $this->get('tags','returnpage');
However, getModel doesn't seem like it's accessible from the view. (Probably the controller only?)
Other, references say to modify the controller (Additional references posted in comments):
https://docs.joomla.org/Using_multiple_models_in_an_MVC_component
Over the last few days, I have tried various iterations of the above referenced code samples . . . Ultimately I am confused about which controller to modify? Do I need to modify the admin controller to get this to work, or the site controller? Would it be easier to add the function to the admin model, and access it on the site view?
It's also been difficult to debug since I don't know which model is throwing the error. My best guess so far though is that I've had NO luck attaching at all to the site model from the admin view. Any help would be appreciated in getting this sorted out.
This is my first question, so I hope that it is clear enough.
I can clarify if needed.
Thanks in advance.

To call a frontend or backend Model you can use JLoader or even require_once to include the model file.
Using JLoader you can call the model inside admin view like this
JLoader::import('joomla.application.component.model'); //Load the Joomla Application Framework
JLoader::import( 'returnpage', JPATH_SITE . '/components/com_stargazer/models' ); //Call the frontend model directory
$tags_model = JModelLegacy::getInstance( 'returnpage', 'StargazerModel' );//Instantiate the model
$tags = $tags_model->gettags();
And you can also use require_once
require_once JPATH_COMPONENT_SITE.'/models/returnpage.php';
$tags_model = JModelLegacy::getInstance( 'returnpage', 'StargazerModel' );//Instantiate the model
$tags = $tags_model->gettags();

Related

How to create 'blog' and new page in Fuel CMS with CI

I am just starting out with fuel CMS and Codeignitor. I'm looking for easy to read suggestions, references, tutorials, code snippets, ANSWERS etc for the 2 following questions below. (2-part Question)
1.) How do I access 'blog' functionality; I've read it is built in as a /view/blog.php but I don't see it; I've tried to create my own (in same directory) but it simply resolves as a static page (I created it from the dashboard) but it lacks any blog > post > get post functionality; like 'blogs' do. I've read time over, like Wordpress and Drupal; Fuel has a 'blog' template. There is none under 'layouts' as well.
So, at this point, I wouldn't mind creating my own 'blog' page - Which leads to:
2.) How do I create a new page manually in Fuel CMS, without the dashboard.
I've created an empty .php file in this directory per documentation:
C:\xampp\htdocs\FUEL-CMS-master\fuel\application\views
I don't really need a custom _variables/ with this -- so what am I missing. I've read I don't need to add / set a new controller with this type of page nor static pages. I also don't want to have to do anything with the controller if I don't need to.
Codeigniter works on CMV Controller - Model - View so to create a simple page you need to create at least 2 files 1 controller and 1 view
if you are using CI 2.2 http://www.codeigniter.com/userguide2/overview/at_a_glance.html
if you are using CI 3 http://www.codeigniter.com/user_guide/overview/at_a_glance.html
first you need to create controller
second create your view
create a file in application/controllhers/blog.php
<?php
class Blog extends CI_Controller {
public function view($page = 'home')
{
//you can acesse this http://example.com/blog/view/
}
public function new($page = 'home')
{
//you can acesse this http://example.com/blog/new/
}
}

How to start a new website project in codeigniter?

I'm really beginner to codeigniter I'm working on CI since last 2 weeks. During this period I have created many views.php files, some controllers.php files and some models.php files
Now I want start a new website project.
What should I do. Should I delete all files of my controllers, views and models, etc., and download another codeigniter and start from the beginning?
You should check the documentation of codeigniter for help but just to give you a quick start ill explain how to create your first codeigniter project.
Installation
1 Download the codeigniter framework from http://ellislab.com/codeigniter
2 upload it in root directory of your website or local apache server directory.
Creating your codeigniter project.
In codeigniter your controller will handle the url requests and load appropriate model and views. So the first step is to create your controller.
1 Creating your controller: go to Applications->controllers and there you will find a built in controller called welcome.php.
This controller loads a view welcome_message.php which is inside Application->views.
You can use this controller or create your own.
To create your own controller create a new php file myfirstcontroller.php and extend a class with same name from CI_Controller.
Note that the name of the file and your class name should be the same. the index function is the default function that will be called when you make a request to the controller
class myfirstcontroller extends CI_Controller {
public function index(){
$this->load->view("myfirstview");
}
}
so when you request this controller through yoursite/index.php/myfirstcontroller
it will load a view called myfirstview.php which will reside inside applications->views.
Go ahead and create this file in applications ->views.
2 To pass data from controller to view you will send an array to the view
class myfirstcontroller extends CI_Controller {
public function index(){
$data['name']="My first application.";
$this->load->view("myfirstview",$data);
}
}
3 You can access this variable in view
echo $name
and it will output your variable
3 you use models you have to create a file inside applications->models and call it from controller and it will return the result in the form of array.
You can look at the documentation for further help.
Hope this helped you to get started with codeigniter.
The user guide is inside your download library.
You can also view it in http://ellislab.com/codeigniter/user-guide/
Good luck!!!
Here is Phil Sturgeon's article on how to do multiple site on one CI instance, in here he explains 2 ways of doing it and describes pros and cons.
http://philsturgeon.co.uk/blog/2009/07/Create-an-Admin-panel-with-CodeIgniter
But in his latest articles he has told what happened to modular separation.
http://philsturgeon.co.uk/blog/2010/03/modular-separation-codeigniter-2

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 ?

Routing problem with Kohana 3.0

in my current project ive a controller tree like this:
Controller -
------Admin -
------------user.php
------otherClass.php
Where controller and admin are folder and user and otherClass are the classes.
If i want to call any otherClass method the url would be this one:
example.com/otherClass
But when i try to call the users methods like this:
example.com/admin/user
i get this : Class controller_admin does not exist (whats logical), so i tried with the routs in the bootstrap.php and after many failed tries, i gave up and decided to ask you guys :P .
The question is how should i code the route::set to make this work.
Thanks
First thing is that you must set 'index_file' key to FALSE in bootstrap (when Kohana::init() is being called).
Leave the default route as it is ( it's controller/action/id, with controller and action being 'index' by default).
For more informations take a look at kohana 3 routing basics article

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