How to access controller method inside the same controller - php

i have method "A_method" inside the controller. and inside A_method i would like to call another method in same controller "B_method".how can i call method "B_method" inside "A_method"
function A_mehtod()
{
$rsl = B_method(10);
echo $rsl;
}
function B_method($data)
{
retun ($data+1);
}
please give me a solution in Php MVC. because i'm working on opencart and i'm new to it

$rsl = $this->B_method($data);
That's all. Have a nice day!!

function A_mehtod() {
$rsl = $this->B_method(10);
echo $rsl;
}

Related

Creating own PHP framework, How to sending parameters to page controller

I'm creating my own framework. It works like this
localhost/controller/action/firstVariable/second/third (And so on...)
My bootstrap look like this:
$request = Util::getInput('request');
$requestList = explode("/",$request);
$modelName = #ucwords($requestList[0]);
$action = #$requestList[1];
$parameters = array_slice($requestList,2);
$controllerName = $modelName.'s'.'Controller';
I'm getting parameters from an url and save them in a variable $parameters. I would like to send them to the current action in my controller the way Laravel 5 is doing.
Example, in Laravel I specify parameters in the url and thats it.
To call them, I need to do a simple step. Just define them:
public function firstAction($first,$second){
}
When I go to an url like:
localhost/Main/firstAction/first/second/
Function of action 'firstAction' will catch those 2 parameters and then basically I can call them inside of the controller and send it to view.
My extends Controller class:
class Controller{
public function __construct($model,$action){
$modelClass = new main();
$reflection = new ReflectionClass($model.'sController');
$reflection->hasMethod($action) ? $this->$action() : die ('Base Controller call error: Method '. $action .' does not exist in Controller '. $model.'sController');
}
public static function renderView($action,$model,$data){
$model = str_replace('sController','',$model);
//include '../application/views/'.$model.'/'.$action.'.php';
$loader = new Twig_Loader_Filesystem('../application/views/'.$model);
$twig = new Twig_Environment($loader);
echo $twig->render($action.'.php', $data);
}
}
class MainsController extends Controller {
private $_data = array();
public function __construct($model,$action){
parent::__construct($model,$action);
}
public function firstAction($first,$second){
echo 'Hoi';
}
}
How can I do it, the good way? I can of course send the variable $parameter to MainController and than call
$this->_data inside of my action but It is not efficient.
I think I need to use arrays to do it, but I have no idea how.
Thank you.
Check out http://php.net/manual/en/function.call-user-func-array.php
P.S.
You do not have to use reflection in order to check if method on that object's instance exist. Single function call can be enough. Check out http://php.net/manual/en/function.is-callable.php
It would be nice if you would use more descriptive names. Now they are confusing.

Passing parameter to controller always get "Page Not Found" - CodeIgniter

I try to pass a parameter to my controller functions from my views but I always get "Page Not Found".
I've been looking for any solutions possible for my problem from here, here, and here but I still couldn't find working solutions and I still don't know what could be the problem. I think I've set everything right, but it's not working. Please tell me if there's something I'm doing wrong.
Here is my controller: Controll.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Controll extends CI_Controller{
public function about($thing){
$case = "About Us";
$data['page'] = $case;
$data['p'] = $thing;
print($thing);
//$this->load->view('/header/header',$data);
$this->load->view('about_us',$data);
$this->load->view('/footer/footer');
}
public function mountain_destination($thing){
$data['page'] = "Mountain Destination";
$data['p'] = $thing;
//$this->load->view('/header/header',$data);
$this->load->view('mountain_destination',$data);
$this->load->view('/footer/footer');
}
}
?>
Here's my routes setting:
$route['(:any)'] = "controll/$1";
$route['default_controller'] = "controll";
$route['404_override'] = '';
And here's one of my script called by onClick that tries to call that controller function:
function goTab(thing,tab){
switch(thing){
case "about":
$body.load("<?php echo site_url('about'); ?>/"+$(tab).text().toLowerCase().replace(/ /g,''));
break;
case "mountain":
$body.load("<?php echo site_url('mountain_destination'); ?>/"+$(tab).text().toLowerCase().replace(/ /g,''));
break;
}
My script runs well and it produces right url. For example, I put parameter "myteam" into "goTab" function, it produces link like this:
"http://127.0.0.1/about/myteam". This link's supposed to call "about" function inside my controller and pass "myteam" as parameter. But instead it returns "Page Not Found". And when I try to call "about" function without any parameters like this: "http://127.0.0.1/about", I get missing argument error like this:
Severity: Warning
Message: Missing argument 1 for Controll::about()
Filename: controllers/Controll.php
Line Number: 22
Please help me.
Thanks in advance.
You're not passing it to your about method. You're looking for a method called myteam within your Controll controller.
Amend your Routes to this;
$route['about/(:any)'] = 'controll/about/$1';
$route['mountain_destination/(:any)'] = 'controll/mountain_destination/$1';
Hope this helps.
This answer of mine might also help you;
Codeigniter Menu from Database
You can create a variable and pass it within constructor and simply use that whenever you need as
class Controll extends CI_Controller {
private $thing;
public function __construct() {
parent::__construct();
$this->thing = $this->uri->segment(3);
}
public function about() {
$data['p'] = $this->thing;
echo $this->thing;
}
public function mountain_destination($thing) {
$data['p'] = $this->thing;
echo $this->thing;
}
}

pass variables from zf2 helper

I want to pass variables to some.phtml from zf2 view helper.
I want to return some data inside my view/application/some/some.phtml
public function __invoke($request)
{
}
can anyone suggest?
Why not just do:
public function __invoke($request)
{
$myVar = 'these';
$myOtherVar = 'probably';
$anotherVar = 'arent really strings';
return [$myVar, $myOtherVar, $anotherVar];
}
But I would probably try to refactor so this is all done in the controller. But if you state the actual problem you are having and what you are trying to achieve you will probably get a better answer

Adding an array of objects to another array in codeigntier PHP?

im using codeigntier framework im trying to solve a problem to do with retrieving information from a database for example:
model.php:
public function read(){
$query = $this->db->get('table');
return $query->result();
}
controller.php:
public function doSomething () {
$exampleArray['name'] = "Bob's Database";
$getModel = $this->load->model('model','getData');
$modelData = $this->getData->read();
// i want to assign the the $modelData to a array element like so
$exampleArray['modelData'] = $modelData // here's where im stuck :(
}
thanks for your help!!
p.s. this is not an error, its just a question :)
}
If you want to be able to access $exampleArray outside of that method, you'll have to do one of two things:
a) set it as a class variable
class MyClass {
public $exampleArray;
.
.
.
}
then refer to it using
$this->exampleArray['index']
or b) pass it by reference into your doSomething() function:
public function doSomething(&$exampleArray) { ... }
The php manual has a section on variable scope that should help you better understand this.

passing variables between functions in same controller using codeigniter

How to pass variable between two functions in same controller?
Do I need to use $this->session->set_flashdata is this right way to go ?
function load() {
$this->userhash = $this->uri->segment(3);
}
function save() {
$query_customer = $this->Customers->get_customer($this->userhash);
}
sorry if this is a basic stuff, but I am still learning.
I need to edit my question
In Ivan link, that I also googled out
there is a code which goes something like this
function index() {
$this->msg = 'data';
$this->testme();
}
function testme() {
echo $this->msg;
}
and its works, but I can not call $this->testme() directly in index(), so I can not load save() in load()
Passing data between two functions in my controller class

Categories