I wanted to use a class variable in symphony controller but it does not work. I want to set it runtime by post method and then retrieve it another function of same controller. I tried with session it works but I dont want to use session.
class TestController extends Controller{
private $test;
public function someAction(){
$testVal = $this->getTest();
print_r($testVal); // its null
}
public function someAnotherAction(){
$this->setTest($_POST["test"]);
}
public function setTest($value){
$this->test= $value;
}
public function getTest(){
return $this->test;
}
}
any help would be appreciated.
You don't call $_POST["test"] inside the class.
Instead use setTest($value) when calling the class
$myclass->setTest($_POST["test"]);
In Symfony controller you can use Symfony requests:
use Symfony\Component\HttpFoundation\Request;
class TestController extends Controller{
private $test;
public function someAction(){
$testVal = $this->getTest();
print_r($testVal); // its null
}
public function someAnotherAction(Request $request){
$this->setTest($request->query->get('test'));
}
public function setTest($value){
$this->test= $value;
}
public function getTest(){
return $this->test;
}
}
Related
I'm trying to concatenate two class spesific variables in controller and pass it to all views without repeating the same variable in every controller methods.
Example code:
class ProductsController extends Controller
{
private $global_path; //Comes from .env
private $sub_folder = '/products_folder';
public function __construct()
{
//Frontend Image Path - to pass into all views
$frontend_path = $this->global_path.$this->sub_folder;
}
}
I want to pass '$frontend_path' to all blade views created in the controller without repeating it in every single method like
return view('example_view', compact('frontend_path');
I tried View::share... but couldn't do it.
The '$sub_folder' variable doesn't have the same value in all controllers.
Is there a way to make it possible?
For your code, I think you can change it to
class ProductsController extends Controller
{
public $frontend_path;
public function __construct() {
$this->frontend_path = env('GLOBAL_PATH') . '/products_folder';
}
public function index()
{
$x = $this->frontend_path;
return view('index', compact('x'));
}
}
and directly use it like $this->frontend_path or like below SELF::$frontend_path
class ProductsController extends Controller
{
public static $frontend_path;
public function __construct() {
SELF::$frontend_path = env('GLOBAL_PATH') . '/products_folder';
}
public function index()
{
$x = SELF::$frontend_path;
return view('index', compact('x'));
}
}
or
class ProductsController extends Controller
{
public static $frontend_path;
public function __construct() {
SELF::$frontend_path = env('GLOBAL_PATH') . '/products_folder';
view()->share('frontend_path', SELF::$frontend_path);
}
public function index()
{
return view('index');
}
}
in view
{{ $frontend_path }}
I am a newbie php coder. I have this main controller:
namespace App\Core;
class Controller
{
/** #var View View The view object */
public $View;
public $templates;
/**
* Construct the (base) controller. This happens when a real controller is constructed, like in
*/
public function __construct()
{
}
public function loadModel() {
$this->model = new \App\Front\Model\IndexModel(); //error line
}
}
In IndexController I have:
namespace App\Front\Controller;
use App\Front\Model\IndexModel;
class IndexController extends \App\Core\Controller {
public function index(){
$this->loadModel->test();
}
}
In IndexModel I have:
namespace App\Front\Model;
class IndexModel
{
public function test(){
echo 'test print';
}
}
In Action I get this error:
Notice: Undefined property: App\Front\Controller\IndexController::$loadModel in /Applications/xampp/htdocs/cmstest/application/Front/Controller/IndexController.php on line 13
I load all classes using composer and PDR-4 method.
What is the problem and how do I fix it? Thanks
Notice that in your loadModel method you just assign the new model to this but you not returning anything -> so you cannot can function test() on null.
In order to fix it use:
class IndexController extends \App\Core\Controller {
public function index(){
$this->loadModel();
$this->model->test();
}
}
If you insist on doing in index only one function you can modify your loadModel function to:
public function loadModel() {
if (!$this->model) // that way you load the Model only once. If you want to reload every time just remove the if
$this->model = new \App\Front\Model\IndexModel();
return $this->model;
}
And then you can do:
public function index(){
$this->loadModel()->test();
}
I have a controller where I have public function as shown below
class Add_project extends MY_Controller
{
public function add_quotation_maker()
{
$this->data['AutomationFullnFinal'] = $this->data['AutomationDiningAndLivingArea'] + $this->data['AutomationLivingArea'] + $this->data['AutomationDiningArea'] + $this->data['AutomationKitchen'] + $this->data['AutomationEntireHome'];
}
}
Now, I have another controller and in which I have another function as shown below
class Make_pdf extends Add_project
{
public function index()
{
}
}
No, I want to use $this->data['AutomationFullnFinal'] from the Add_project controller into the Make_pdf controller inside the public function index.
Any help is welcome.
Hope this Help You with little tweak :
class Add_project extends CI_Controller
{
public function add_quotation_maker()
{
$this->data['AutomationFullnFinal'] = $this->data['AutomationDiningAndLivingArea'] + $this->data['AutomationLivingArea'] + $this->data['AutomationDiningArea'] + $this->data['AutomationKitchen'] + $this->data['AutomationEntireHome'];
}
public function test()
{
echo "string";;
}
}
In Make_pdf file just include the above file :
include_once (dirname(__FILE__) . "/Add_project.php");
class Make_pdf extends Add_project {
public function test2()
{
$this->test();
}
}
now you can access the methods of Add_project class:
It will achieve by below steps:
1> extend required class in controller.
2> Call that function by extended controller's object.
It would appear from the use of $this->data that the Add_project class has declared the property $data. If the visibility of $data is declared as public or protected it be usable in Make_pdf and accessed the same way e.g. $this->data['whatever'].
The declaration might look something like
class Add_project extends CI_Controller
{
public $data;
public function add_quotation_maker()
...
I am following this link to implement it
I did below steps to implement the Contract in my existing class.
Below is the class where I will write some logic also before sending it to controller
namespace App\Classes\BusinessLogic\Role;
use App\Classes\DatabaseLayer\Role\RoleDb;
use App\Classes\Contract\Role\IRole;
class RoleBL implements IRole {
public function All() {
return (new RoleDb())->All();
}
}
Database Function
namespace App\Classes\DatabaseLayer\Role;
class RoleDb {
public function All() {
$Roles = \App\Models\Role\RoleModel
::all();
return $Roles;
}
}
Interface
namespace App\Classes\Contract\Role;
interface IRole {
public function All();
}
Service Provider class
namespace App\Providers\Role;
class RoleServiceProvider extends \Illuminate\Support\ServiceProvider {
public function register()
{
$this->app->bind('App\Classes\Contract\Role\IRole', function($app){
return new \App\Classes\BusinessLogic\Role\RoleBL($app['HttpClient']);
});
}
}
Finally in config/app.php in provider wrote below line.
App\Providers\Role\RoleServiceProvider::class
Controller - Constructor
protected $roles;
public function __construct(\App\Classes\Contract\Role\IRole $_roles) {
parent::__construct();
$roles = $_roles;
}
Controller Action method
public function index(IRole $roles) {
$RoleTypes = $roles->All();
}
So far everything works fine if I keep Interface as parameter in method.
if I try to use the variable $roles in index method and remove the variable, it is always null.
Please guide me if I missed anything?
You incorrectly assign the $roles property in your __construct() method.
Replace
$roles = $_roles;
with
$this->roles = $_roles;
and then in your index method do:
$RoleTypes = $this->roles->All();
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()
{
}
}