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;
}
}
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();
}
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 using hierarchical MVC model in codeigniter. I create a controller called template and inside it a function called sample_template. Then a view called sample_template_v created and call it inside the template controller. I create another controller called Admin and called Template->sample_template($data); inside its 2 funtions.
MY_Controlle.php
<?php
class MY_Controller extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->module('Template');
}
}
Admin.php
<?php
class Admin extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$data['content_view'] = 'admin/admin_v';
$this->Template->sample_template($data);
}
function about()
{
$data['content_view'] = 'admin/about_v';
$this->Template->sample_template($data);
}
}
Template.php
<?php
class Template extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function sample_template($data = NULL)
{
$this->load->view('Template/sample_template_v', $data);
}
}
sample_template_v.php file---->
<h5>This is the main Template.</h5>
<?php $this->load->view($content_view); ?>
Error:
If you want to call a method from the object you need to initiaze the object and then call a method. Make sure $this->template is set in your case it isn't.
$this->template = new Template();
$this->template->sample_template($data);
I have this route: Route::controller('/', 'PearsController'); Is it possible in Laravel to get the PearsController to load a method from another controller so the URL doesn't change?
For example:
// route:
Route::controller('/', 'PearsController');
// controllers
class PearsController extends BaseController {
public function getAbc() {
// How do I load ApplesController#getSomething so I can split up
// my methods without changing the url? (retains domain.com/abc)
}
}
class ApplesController extends BaseController {
public function getSomething() {
echo 'It works!'
}
}
You can use (L3 only)
Controller::call('ApplesController#getSomething');
In L4 you can use
$request = Request::create('/apples', 'GET', array());
return Route::dispatch($request)->getContent();
In this case, you have to define a route for ApplesController, something like this
Route::get('/apples', 'ApplesController#getSomething'); // in routes.php
In the array() you can pass arguments if required.
( by neto in Call a controller in Laravel 4 )
Use IoC...
App::make($controller)->{$action}();
Eg:
App::make('HomeController')->getIndex();
and you may also give params
App::make('HomeController')->getIndex($params);
You should not. In MVC, controllers should not 'talk' to each other, if they have to share 'data' they should do it using a model, wich is the type of class responsible for data sharing in your app. Look:
// route:
Route::controller('/', 'PearsController');
// controllers
class PearsController extends BaseController {
public function getAbc()
{
$something = new MySomethingModel;
$this->commonFunction();
echo $something->getSomething();
}
}
class ApplesController extends BaseController {
public function showSomething()
{
$something = new MySomethingModel;
$this->commonFunction();
echo $something->getSomething();
}
}
class MySomethingModel {
public function getSomething()
{
return 'It works!';
}
}
EDIT
What you can do instead is to use BaseController to create common functions to be shared by all your controllers. Take a look at commonFunction in BaseController and how it's used in the two controllers.
abstract class BaseController extends Controller {
public function commonFunction()
{
// will do common things
}
}
class PearsController extends BaseController {
public function getAbc()
{
return $this->commonFunction();
}
}
class ApplesController extends BaseController {
public function showSomething()
{
return $this->commonFunction();
}
}
if you were in AbcdController and trying to access method public function test() which exists in OtherController you could just do:
$getTests = (new OtherController)->test();
This should work in L5.1