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();
}
Related
I trying to implements a interface to a controller but when i try that, the request is converted into a string.
Here is the code of the controller:
class FilesController extends Controller implements Repository
{
function __construct()
{
$this->factory = new RepositoryFactoryImp();
}
public function index($request)
{
$repository = $this->factory->createRepository($request->type_repository);
return $repository->getFilesList($request);
}
}
Here is the code of the interface:
interface Repository
{
public function index(GetFileListRequest $request);
}
Then the error that i get is:
ErrorException: Trying to get property 'type_repository' of non-object
in file
C:\xampp\htdocs\pocs\repository\app\Http\Controllers\FilesController.php
on line 31
I do a dd($request); and the result is a string, the string is the content of type_repository variable of the route:
Route::get('files/{type_repository}', 'filesController#index');
What can be the problem? Is possible to implements a interface to a controller?
Well to get started you haven't injected the request in your controller:
class FilesController extends Controller implements Repository
{
// ...
public function index($request) // <-----
{ // ^^^^^^^^^
$repository = $this->factory->createRepository($request->type_repository);
return $repository->getFilesList($request);
}
}
Try doing this instead:
use Illuminate\Http\Request;
// ...
public function index(Request $request) { ... }
// ^^^^^^^^^^^^^^^^
Side note
As an observation, you have declared the index() method in your interface but you are calling the createRepository() one in your implementation.
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 have a problem with the following code. When I use $this when not in object content.
dashboardController.php
<?php
class dashboardController extends BaseController{
public function index($name=''){
$this->view->loadView('dashboard/index');
}
}
baseController.php
<?php
class BaseController{
public function loadView($viewName){
$this->view = new View($viewName);
}
}
view.php
<?php
class View{
public function __construct($viewName);{ echo " i am form view to render
}
}
I am getting the error using $this when not in object content but in another folder the login went success without.
Try this:
dashboardController
<?php
class dashboardController extends BaseController
{
public function index($name = '')
{
// Call loadView(), because this controller doesn't have a method
// called loadView() it'll fall back to loadView() in the BaseController
$this->loadView('dashboard/index');
// Get the viewName from the view, will be 'dashboard/index'
echo $this->view->getViewName();
}
}
BaseController
<?php
class BaseController
{
// The view, protected so it can be accessed by children via $this->view
protected $view;
// Load a view, protected so it can only be accessed by children
// or within this controller
protected function loadView($viewName)
{
// Create a new view instance
$this->view = new View($viewName);
}
}
View
<?php
class View
{
// The loaded view, private so it can't be changed by an external class
private $viewName;
public function __construct($viewName)
{
$this->viewName = $viewName;
}
// Retrieve the view name, public so anything can access it
public function getViewName()
{
return $this->viewName;
}
}
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 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;
}
}