I know this is MVC structure incompatible but i need to use this technique:
I have a controller and a model.
I'm calling a function in model from controller.
Model called function calles controllers another function. (This is what generates error).
Example below:
Controller:
public function B($ret=false) {
if(!$ret)$this->Model_model->M($ret);
else echo 'ok';
}
Model:
public function M($ret=false) {
$this->N($ret);
}
private function N($ret=false) {
$this->Controller->B(!$ret); //i can't find how can i call this
}
My first trigger function is:
$this->Controller->B(false);
I've moved code in function B of controller to the Model completely and now everything is going in model itself. B function calling another B function in model and at last step model isn't need to call controller; it is calling B function in Model:
Controller:
public function B($ret=false) {
$this->Model_model->B($ret);
}
Model:
public function B($ret){
if(!$ret)$this->M($ret);
else echo 'ok';
}
private function M($ret=false) {
$this->N($ret);
}
private function N($ret=false) {
$this-B(!$ret);
}
My first trigger function is:
$this->B(false);
And my controller is still has short code.
Related
I'm calling the endpoint that routes to methodIWantToTest like so:
$response = $this->json('GET', 'my/endpoint/');
I'm attaching the code below, any ideas how I can mock the call to the second method?
Thanks.
class MyController extends Controller
{
public function methodIWantToTest():
{
//some code to test
$this->methodIWantToMock()
//some more code to test
}
public function methodIWantToMock():
{
//mock this response
}
}
I don't know if I understand your question correctly but you're already doing it.
I don't know also why you're using ':' after the '()' in function and you need semicolon after you call the method you want to call
class MyController extends Controller
{
public function methodIWantToTest()
{
//some code to test
$this->methodIWantToMock();
//some more code to test
}
public function methodIWantToMock()
{
//mock this response
}
}
you can also pass value if you want just do this
class MyController extends Controller
{
public function methodIWantToTest()
{
//some code to test
$this->methodIWantToMock($value);
//some more code to test
}
public function methodIWantToMock($value)
{
//mock this response
}
}
I am new to laravel. I am having a doubt how to call other function inside the same controller and return the processed values to the function by which it has been invoked. I have tried this similar to C language but the code doesn't works
class AgreementsApiController extends Controller
{
public function store($th_id,$mv_id,$wk1_terms,$wk2_terms,$wk3_terms)
{
//make a function call here to add function similar to
$result=add($th_id,$mv_id);
}
public function add($th_id,$mv_id)
{ //process the parameters and return to store function
$r=$th_id+$mv_id;
return $r;
}
}
In your store function
$result=$this->add($th_id,$mv_id);
and thats it.
contrller:News.php
This is my controller News
<?php class News extends CI_Controller {
public function __construct()
{
}
public function getShowIN_News()
{
return $result;
} } ?>
contrller:Category.php
This is my controller Category
<?php class Category extends CI_Controller {
public function __construct()
{
}
public function category()
{
require('news.php');
$test = new News();
$data["headlines"] = $test->getShowIN_News();
} }?>
By using an empty constructor, you're making it so that CI_Controller::__construct() isn't called, and that's where everything in the framework is initialized.
I know you've put it there to hack it so you can call one controller from another, but it is very intentionally made that way, exactly so you don't do this.
I am new into Phalcon framework. I just got the basic idea about it. Every controller has methods with multiple specific actions. I wrote a huge indexAction method but now I want to break it down with multiple private method so that I can reuse those functionality. But when I try to create any method without action suffix, it returns error(Page Not Found). How can I break it down into multiple methods?
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
$this->someMethod();
}
public function someMethod()
{
//do your things
}
}
Controllers must have the suffix “Controller” while actions the suffix “Action”. A sample of a controller is as follows:
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
}
public function showAction($year, $postTitle)
{
}
}
For calling another method, you would use it straight forward
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
echo $this->showAction();
}
private function showAction()
{
return "show";
}
}
Docs.
What exactly do you want? The answer seems trivial to me.
class YourController extends Phalcon\Mvc\Controller
{
// this method can be called externally because it has the "Action" suffix
public function indexAction()
{
$this->customStuff('value');
$this->more();
}
// this method is only used inside this controller
private function customStuff($parameter)
{
}
private function more()
{
}
}
how to call model method in another model, example
I have code like this
/model/user.php
public function get_token_by_id($id){
//some code
}
i want call in my another model
/model/restaurant
App::bind('user','user');
class RestaurantController extends BaseController {
public function __construct(user $modelUser){
$this->modelUser = $modelUser;
}
public function getUser(){
$someVar = $this->modelUser->get_token_by_id($id);
}
}
But i get an error
Call to a member function get_token_by_id() on a non-object
how to fix it?
Well... that's because $this->modelUser is a non object !
To be more precise, $this->modelUser returns null or something like that (try a var_dump($this->modelUser)). It could be because your model doesn't have the attribute declaration (protected $modelUser) or because you don't pass the right variable into the constructor.