How to call custom controller from index controller in Zend Framework - model-view-controller

I am newbie in Zend framework.And i have made sample project in netbeans.And it is working properly displaying index.phtml .But , I need to call my controller.What I have tried is below.
IndexController.php
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
firstExample::indexAction();
}
}
And I have deleted all the content of index.phtml(just a blank file) , coz i don't want to render this view.
My custom controller is:
firstExampleController.php
<?php
class firstExample extends Zend_Controller_Action{
public function indexAction(){
self::sum();
}
public function sum(){
$this->view->x=2;
$this->view->y=4;
$this->view->sum=x + y;
}
}
?>
firstExample.phtml
<?php
echo 'hi';
echo $this->view->sum();
?>
How to display sum method in firstExample.php.
It just shows blank page after hitting below URL.
http://localhost/zendWithNetbeans/public/
I think after hitting on above URL , execution first goes to index.php in public folder.And I didn't change the content of index.php

You are using controller (MVC) incorrectly, Controller should not do any business logic, in your case sum method. Controller only responsible controlling request and glueing model and view together. That's why you have problems now calling it.
Create Model add method sum, and use in any controller you want. From controller you may pass model to view.
Here is example: http://framework.zend.com/manual/en/learning.quickstart.create-model.html it uses database, but it's not necessary to use with database.
Basically your sum example could look like:
class Application_Sum_Model {
public function sum($x, $y) {
return ($x + $y);
}
}
class IndexContoler extends Zend_Controller_Action {
public function someAction() {
$model = new Application_Sum_Model();
//passing data to view that's okay
$this->view->y = $y;
$this->view->x = $x;
$this->view->sum = $model->sum($x, $y); //business logic on mode
}
}
Please read how controller is working, http://framework.zend.com/manual/en/zend.controller.quickstart.html

Related

Call a method from one controller inside another

Is it possible to call a method from one controller inside another controller in Laravel 5 (regardless the http method used to access each method)?
This is how I have done it. Use the use keyword to make the OtherController available. Then you can call a method from that class on instantiation.
<?php namespace App\Http\Controllers;
use App\Http\Controllers\OtherController;
class MyController extends Controller {
public function __construct()
{
//Calling a method that is from the OtherController
$result = (new OtherController)->method();
}
}
Also check out the concept of a Command in Laravel. It might give you more flexibility than the method above.
use App\Http\Controllers\TargetsController;
// this controller contains a function to call
class OrganizationController extends Controller {
public function createHolidays() {
// first create the reference of this controller
$b = new TargetsController();
$mob = 9898989898;
$msg = "i am ready to send a msg";
// parameter will be same
$result = $b->mytesting($msg, $mob);
log::info('my testing function call with return value' . $result);
}
}
// this controller calls it
class TargetsController extends Controller {
public function mytesting($msg, $mob) {
log::info('my testing function call');
log::info('my mob:-' . $mob . 'my msg:-' . $msg);
$a = 10;
return $a;
}
}

How to create login / registerform template with auth_tank/codeigniter?

Struggling to figure out how to achieve this. I want a login/registerform on same page when using tank_auth in CodeIgniter.
I was thinking of having something like this in my members controller (which extends My_Controller which extends Auth).
class Members extends My_Controller (
public function login()
{
//Already loggedin, do nothing
if ($this->tank_auth->is_logged_in()) {
return;
}
$this->view_data['login_by_username'] = true;
$this->view_data['login_by_email'] = false;
$this->view_data['show_captcha'] = false;
$this->view_data['login_form'] = $this->load->view('auth/login_form', $this->view_data, TRUE);
$this->view_data['register_form'] = $this->load->view('auth/register_form', $this->view_data, TRUE);
$this->v('members/login_register'); //In this view $login_form and $register_form is rendered
}
}
When I go to that page (localhost/members/login) the members/login-view is shown as expected, but in the action of the form for loginform it says:
http://localhost/members/login
Therefore when I click "Login-button" then it just calls members/login and that's not what I want. I want the action to be auth/login, but I still want to use my own template.
I hope you guys understand what I mean.... Please tell me if I'm doing anything wrong/thinking of this incorrectly.
UPATE:
In the actual template (members/login_register) it looks like this:
<div class="column">
<?php echo $login_form;?>
</div>
<div class="column>
<?php echo $register_form;?>
</div>
Maybe what I want to achieve is not possible?
Master View
You should use a Master view as a wrapper for your content.
That way you can easily pass in partial views(although not required)
However it will keep things neat. It also allows for easier control over
Admin/User Dashboards and your frontend.
Main Controller
class MY_Controller extends CI_Controller
{
public $template;
public function __construct()
{
$this->template = "master/template"; //views/master/template
}
}
To change it for an admin template(as an example of flexibility) you simply need to change it
the variable in the __constructor
class Admin_Controller extends MY_Controller
{
public function __construct()
{
$this->template = "master/admin/template"; //views/master/admin/template
}
}
Partial View(s)
Partial views can be used directly in other views.
You don't need a controller to call them.
These views are just stored in the buffer when they are
loaded by the loader class $this->load->view('', '', true)
A common approach is to create a folder inside /views called "partials".
This is where you would keep all you re-usable views(such as forms/widgets).
The Views(/views/partials/login_form)
<div>
<?php echo form_open('members/login', array('id'=>'login-form'))
</div>
The Views(/views/partials/signup_form)
<div>
<?php echo form_open('members/signup', array('id'=>'signup-form'))
</div>
The Views(members/index)
You can then combine the views
<div class="signup-form">
<?php echo $this->load->view('partials/signup_form')
</div>
<div class="login-form">
<?php echo $this->load->view('partials/login_form')
</div>
Login / Signup
In your members class you can create one method to show
the register/login form and then create methods to handle each of them individually.
class Members extends MY_Controller
{
public function index()
{
return $this->load->view('members/login_register');
}
public function login()
{
if(!$this->form_validation->run()){
return $this->index();
}
// form validation passed Ask Tank Auth
// to log the user in
$tank->auth->login();
}
public function signup()
{
if(!$this->form_validation->run()){
return $this->index();
}
// form validation passed Ask Tank Auth
// to register
$tank->auth->register();
}
}

Redirecting to own controller methods

Signup controller:
<?php
class Signup extends BaseController
{
public function getNew()
{
return Redirect::action('Signup#success');
}
public function success()
{
echo "successful";
}
}
?>
Routes.php
Route::controller('signup', 'Signup');
So when I go to localhost/signup/new it should throw out successful but ends with Unknown action [Signup#success]. error.
I read many same topics before but didn't help me at this case.
you need to use a certain format.. see RESTful Controllers
it says "Next, just add methods to your controller, prefixed with the HTTP verb they respond to"
change your Signup controller to:
<?php
class Signup extends BaseController
{
public function getNew()
{
return Redirect::action('Signup#getSuccess');
}
public function getSuccess()
{
echo "successful";
}
}
?>
and everything should work now..

White Screen When posting in CodeIgniter

Codeigniter gives a white screen every time a form is posted:
Here is the controller logic [controllers/account.php]:
class Account extends CI_Controller
{
public function create()
{
if($this->input->post(NULL, TRUE)){
$params = $this->input->post();
//add validation layer
$accountOptions = array($params are used here)
$this->load->model('account/account', 'account');
$this->account->initialize($accountOptions);
$this->account->save();
}
$header['title'] = "Create Free Account";
$this->load->view('front_end/header', $header);
$this->load->view('main_content');
$content['account_form'] = $this->load->view('forms/account_form', NULL, TRUE);
$this->load->view('account/create', $content);
$footer['extraJs'] = "account";
$this->load->view('front_end/footer', $footer);
}
}
Here is the Account Model logic [models/account/account.php]:
class Account extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function initialize($options)
{
//initialize
}
}
The view first loads fine then after filling the form and clicking submit, just white page.
I tried to add __construct to the controller and load account/account from there, the form does not even load. Any ideas?
I just found the problem:
- The Model account has duplicated definition and the error_reporting was off!
You shouldn't have two classes with the same name Account (the controller and model). Check your server and/or Codeigniter log it should show up there.
I advise you to call your controller class Account and your model class M_Account. You can then rename the model to account whenever you load it, just like you did:
$this->load->model('account/m_account', 'account');
public function __construct()
{
parent::__construct();
$this->load->model("account/account");
}
you load model,library and helper files in construct dont load to inside a function

Variable from controller not send to view in CodeIgniter

I am using HMVC with CodeIgniter.
I have this in my testmodule controller:
public function index()
{
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
And this in my view template.php of that controller that is loaded by this controller:
<?php
$this->load->view('includes/header');
$this->load->view($main_content);
$this->load->view('includes/footer');
?>
but, when I var_dump($main_content) in the view and die() it shows null instead of frontpage
How, come? I don't get it at all.
If you want to use $this->view_data you have to declare $view_data as a property first (at the top of your controller):
class TestModule extends CI_Controller
{
public $view_data = array();
public function index()
{
// Now you can use $this->view_data in this function:
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
}

Resources