I want to pass current path info, i.e controller/action/params, to a widget that uses another model to generate some static HTML contents in the views of the application, including the layout too.
In-order to achieve that goal, I have defined the following controller AppController and I make all of my app controllers extends it instead of yii\web\Controller;
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
class AppController extends Controller
{
public function __construct($id, $module, $config = array()) {
parent::__construct($id, $module, $config);
$this->view->params['path2'] = Yii::$app->controller->id;
}
/*
public function beforeAction($action) {
parent::beforeAction($action);
$this->view->params['path2'] = Yii::$app->controller->id;
} */
}
And, for example, in SiteController:
class SiteController extends AppController
{...
The problem is in any view of any SiteController action, for eaxmple about echo $this->params['path2'] returns blank while I expect it to echo site. In addition, uncommenting beforeAction makes all SiteController actions produces blank pages.
I need any way to allow, globally, set $this->params['path2'] value to be the current controller/action/params regardless what routs rewrites is.
Related
I think we have separate controllers respective to separate logic or modules of our application, and i have also found that using a controller inside another controller is not a good practice.
Here i have facing a difficulty.
there are two controllers PagesController and PostsController
PagesController handle all pages related tasks.
class PagesController
{
public function index()
{
// method of our root request, get and show all posts
}
public function contactUs()
{
// show contact us page etc.
}
}
PostsController handle all posts related tasks.
class PostsController
{
public function getPosts() {} // get all posts from database
public function deletePost($id) {} // delete a post
public function editPost($id) {} // edit a post
}
Now post controller handle all posts specific tasks and pages controller handle all pages related tasks. The problem is that i want to use posts controller getPosts() method to get all posts and pass them to view. How can i use PostsController's getPosts() method inside our PagesController index() method.
One way is extends PostsController and use it. But what if we want to use another controller's method also.
Please provide me better way to do that.
https://www.youtube.com/watch?v=MF0jFKvS4SI
This talk is a bit advance about controllers but it is surely a good practice regarding controllers.
better to create a trait......
How to use traits in Laravel 5.4.18?
You can use XyzController method in any controller by following way
use App\Http\Controllers\XyzController ;
class AnyController extends Controller {
public function functionName()
{
$result = (new XyzController)->methodName();
// this will call method of XyzController
}
}
Hope this will help.
Your controller should not have any logic. Create a service or create a method in Repository and move PostsController's getPosts()'s code into this method. And then call this new method in both PostsController and PageController.
The whole point of having Repository is for this purpose.
I usually prefer Repository pattern to get task done.
Here's an Overview.
interface BaseMethodsForRepository {
/**
* #return mixed
*/
public function get();
//other base methods like store (handle create/update in common method) and delete.
}
class PostRepository implements BaseMethodsForRepository {
public function get() {
return Post::all();
}
//Many more methods
}
class PagesRepository implements BaseMethodsForRepository {
public function get(){
return Page::all();
}
}
class PageController {
private $postRepository
public function __construct(PostRepository $postRepository) {
$this->postRepository = $postRepository;
}
public function index(){
//here you can use all public methods of PostRepository
//usage
$post = $this->postRepository->get();
}
}
I found this useful and code is reusable.
I want to change layout for all views inside my controller. So I want to set like this:
class SiteController extends Controller {
public function __construct(){
$this->layout = 'admin';
}
.....
But I am getting the following error:
Call to a member function getUniqueId() on null
By default yii2 uses the main layout as the layout for project and it's controllers, but if you want to use another layout for a controller or change the layout name and use that layout for a controller to have to define a layout property in your controller class and give the name of your layout as the string value of that property.
This will change the layout of that controller to the demanded layout with your chosen name.
Here is the code in your case:
class SiteController extends Controller
{
public $layout = '[Your Layout Name]';
.
.
.
}
P.S: Constructor is the method which runs whenever you create an instance of your class and using it in this case is not logical.
I used init() and worked as expected. I changed my code to:
class SiteController extends Controller {
public function init() {
$this->layout = 'admin';
}
....
Your sitecontroller extends to parent Controller. So you can create constructor in parent controller but not in sitecontroller....
try using
public function init(){
}
in site controller
I am using Codeigniter framework to develop a website. I am currently working on home.php view under the view folder. I need to use UserInfo() function which is inside one of the controllers. Any suggestion how to access that function?
class Welcome extends CI_Controller {
public function UserInfo(){
$this->load->model('model_user');
$data['title'] = 'Users';
$data['users'] = $this->model_user->getUser();
$this->load->view('template/users', $data);
}
}
You cant call controller method inside another controller. Its No Way to do it.
You have two way to resolve this issue
If you want to access the function which place inside the
controller, add that into an model. So by loading model you can call
it.
use redirect('welcome/UserInfo') if you just need to call the function
As You want to call controller function in other controller.In codeigniter App folder core folder exists you make a custom core controller and all other controllers extend with your custom controller
In your Custom Core controller
class CustomCore extends CI_Controller
{
/* ---YOUR FUNCTION IN CUSTOMCORE---- */
public function mycorefunc()
{
//Do something
}
}
and your all other controllers extend with custom core
class YourController extends Customcore
{
function controllerfunction()
{
$this->mycorefunc();// Call corefunction
}
}
I am creating an API and I want to include both regular resources and nested resources
For example, I will say I have a Post resource and Comment resource. I have setup the appropriate routes and controllers like the following
Routes
Route::resource('posts', 'PostsControllers'); // /posts/{id}
Route::resource('comments', 'CommentsControllers'); /comments/{id}
But I also want to have comments as a nested resource of posts, like this
Nested resource route
Route::resource('posts.comments', 'PostCommentsControllers'); /posts/{id}/comments/{id}
Because I have already written my CommentsController, I would like to know of the best method to re-use the CommentsController for my PostsController
Thanks
Using inheritance is the best way:
class BaseController extends Controller {
public function index() {
}
public function create() {
}
public function store() {
}
public function update() {
}
}
class PostsController extends BaseController {
}
class CommentsController extends BaseController {
}
You can just extend your Blog/Comment/*Controller on a generic FooBarController which holds all the logic.
You will have to supply the model and other model-related data, I do this via the constructor, and my models holding data about the columns, etc.
Is it possible to call the member function of another controller in zend framework, if yes then how?
<?php
class FirstController extends Zend_Controller_Action {
public function indexAction() {
// general action
}
public function memberFunction() {
// a resuable function
}
}
Here's another controller
<?php
class SecondController extends Zend_Controller_Action {
public indexAction() {
// here i need to call memberFunction() of FirstController
}
}
Please explain how i can access memberFunction() from second controller.
Solution
Better idea is to define a AppController and make all usual controllers to extend AppController which further extends Zend_Controller_Action.
class AppController extends Zend_Controller_Action {
public function memberFunction() {
// a resuable function
}
}
class FirstController extends AppController {
public function indexAction() {
// call function from any child class
$this->memberFunction();
}
}
Now memberFunction can be invoked from controllers extending AppController as a rule of simple inheritance.
Controllers aren't designed to be used in that way. If you want to execute an action of the other controller after your current controller, use the _forward() method:
// Invokes SecondController::otherActionAction() after the current action has been finished.
$this->_forward('other-action', 'second');
Note that this only works for action methods (“memberAction”), not arbitrary member functions!
If SecondController::memberFunction() does something that is needed across multiple controllers, put that code in a action helper or library class, so that both controllers can access the shared functionality without having to depend on each other.
You should consider factoring out the code into either an action helper or to your model so that it can be called from both controllers that need it.
Regards,
Rob...
I would suggest you to follow DRY and move those functions to common library place. For example create in library folder
My/Util/
and file
CommonFunctions.php
then call your class
My_Util_CommonFunctions
and define your methods
Now you can call them from any place in the code using your new namespace which you have to register.
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace(array('My_'));
in any controller you can call your custom methods by using:
My_Util_CustomFunctions::yourCustomMethod($params);