I'm in a lithium layout file and I'd like to echo the name of the current controller (to use as a CSS class later). How can I obtain the current controller name?
Thanks,
aeno
I assume you mean you are in a View?
If so, it's pretty simple to get the controller or other parts of the route/request ...
<?=$this->_request->controller;?>
That will get you the Controller but you can get just about anything from your route you'd need. So assuming you have a route like ...
Router::connect('/{:controller}/{:action}/{:id}');
You can use both of the following in your view:
<?=$this->_request->action;?>
<?=$this->_request->id;?>
Or you could have a fancier route like ..
Router::connect('/{:id}/{:area}/{:controller}/{:action}/');
This would be for a url like:
http://mysite.com/123/media/photos/edit/
Now you can do something like ...
<?=$this->_request->area;?>
To get the "area" portion of the url, etc. You get the idea.
The following code can be used in any Lithium layout or view to look up the current Controller, convert it to a suitable CSS class name, and set it as the class attribute of a div:
<?php
$controller = $this->request()->controller;
$controller_css_class = strtolower(\lithium\util\Inflector::slug($controller));
?>
<div class="<?=$controller_css_class; ?>"></div>
The request class is documented here: http://li3.me/docs/lithium/action/Request
Related
I developed a project in php codeigniter. The project is almost complete but now my manager wants me to completely remove or hide the controller names in the URL. My current URL looks like this:
http://www.sitename.com/Controller_Name/function_name
and my manager wants it to look like this:
http://www.sitename.com/function_name
Note that I have more than 10 controllers in my project and many many functions. I want a method that applies to all. HELP PLEASE.
You can do this using $route.
If you have only one Controller and in that controller you all the functions you can write something like this:
$route['(:any)'] = "Controller_Name/$1";
If you have many Controllers you need to specify for each function to what controller to point. Like this:
$route['function1'] = "Controller_Name1/function1";
$route['function2'] = "Controller_Name1/function2";
$route['function3'] = "Controller_Name3/function3";
And you can't have duplicates in $route
This $route array should be located here: application/config/routes.php.
You can check more about routes on the CI documentation here: https://www.codeigniter.com/userguide3/general/routing.html
You have to specify your controller and method name in routes file like below
$route['function_name'] = 'controller_name/function_name';
You have to write this for each function in controller.By doing you get two advantages
1) Controller name will be hidden in your url
2) you can call some method by its name only.No need to use controller name every time.
I have a view that is rendered with its controller. The function that calls the view is linked in my routes. It works fine when directly accessing the route, but obviously my controller is not included when I include it in my template.
How do I use my controller when I include my view?
I'm on Laravel 3.
Right now I have my controller :
public function get_current()
{
// $sales = ...
return View::make('sale.current')->with('sales',$sales);
}
My route (which obv only work on GET /current) :
Route::get('current', 'sale#current');
My master view
#include('sale.current')
Then my sale.current view calls $sales
#foreach($sales as $sale)
Thanks!
So this is the case when you want to call some laravel controller action from view to render another partial view. Although you can find one or another hack around it. However, please note that laravel controllers are not meant for that.
When you encounter this scenario when you want to reuse the same view again but don't want to supply all necessary data again & again in multiple controller actions, it's the time you should explore the Laravel View Composers.
Here is the official documentation link : https://laravel.com/docs/master/views#view-composers
Here is the more detailed version of it :
https://scotch.io/tutorials/sharing-data-between-views-using-laravel-view-composers
This is the standard way of achieving it without any patch work.
Your question is still unclear but I can try to help you. I did a small example with the requirements you gave. I create a route to an action controller as follows:
Route::get('test', 'TestController#test');
In TestController I define the action test as follows:
public function test()
{
return View::make('test.home')->with('data', array('hello', 'world', '!'));
}
According to your asking, you defined a view who includes content from another view (layout) and in that layout you use the data passed for the action controller. I create the views as follows:
// home.blade.php
<h1>Message</h1>
#include('test.test')
and
// test.blade.php
<?php print_r($data); ?>
When I access to "test" I can see print_r output. I don't know if that is what you are doing, but in my case works fine.
I hope that can help you.
I want to get the current controller name that handles the current action.
but the in my case I will look for the current controller in my main.php in my layout files.
this is my small view of my directory structure to give you an idea where is my layout files and the file where i will put my codes in searching of my controller name
/protected
/themes
/mylayout
/layouts
main.php
column1.php
column2.php
/site
index.php
Is this possible? im trying the following codes but i failed to get my current controller name...
echo Yii::app()->controller->getId;
echo Yii:app()->getController->id;
echo Yii:app()->controller->uniqueID;
thanks
Like this
Yii::app()->controller->id
or
Yii::app()->getController()->getId()
http://www.yiiframework.com/doc/api/1.1/CApplication#getController-detail
Controller Id :
$this->id
Here $this refers to controller.
And
For getting action id :
$this->action->id
<?php echo $this->getUniqueId();?>
this will show current controller
You're not actually required to use the static function. Whenever in a view( or template) you can use echo $this->getUniqueId(); to get the unique controller ID.
Yii2:
Yii::$app->controller->id
(Documentation: Application and Controller)
I have Entry controller with index action that lists all entries, and of course views/scripts/index.phtml. I also have the main page index/index.phtml. How can I include entry/index.phtml in index/index.phtml so I can see the results of entries as part of the structure of the home page?
try something like this towards the end of your indexAction() in the index controller:
$this->_helper->actionStack('index', 'entry');
Alternatively, I think you may be able to to do think in the index/index.phtml script:
<?php echo $this->action('index', 'entry');?>
First example is the actionStack action helper the second is the action view helper
Good luck!
You may create view helper for this, in which you:
retrieve the data (e.g. from database)
pass the data to the view
render the view you need ($this->view->render('pathoto/scriptname.phtml')). You may also add script path using addScriptPath().
Then use this helper in those two scripts you need.
If AJAX it the root of your needs, take a look at actionContext and ajaxContext action helpers.
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html
I have a controller that is called with AJAX (sends JSON data), so I don't use a view.
I need to use a personnal view helper to format my data, but in my controller.
Is that possible ?
Or maybe I am doing it wrong (maybe I should have a view, but how with JSON) ?
You can access any ViewHelper from the Controller by
$this->view->helpername(/*params*/);
// or
$helper = $this->view->getHelper('helpername');
// or
$broker = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$broker->getView()->helpername(/*params*/);
See Zend: How to use a custom function from a view helper in the controller?
However, you might be right that you are doing it wrong (funny pic btw), but I cannot really tell from your question. Please refine it as to why you need to call the view helper and what it is supposed to format.
Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
Just be sure that the returned view is the view you want. Because down the line, the view may get overwritten and on the controller you have a spank new view.
And all those values you setup on the view on the action helper and the like... before the controller is kicked in? All gone with the wind!
So test before assuming that if you get a view resource. it is really the same view resource you expect, and that all your vars are still there.
You may be surprised as i was!
You can create an instance of a Helper .. this will work in Controllers, Models and everywhere you need the Helper.
eg.
// create Instance
$serverUrl_helper = new Zend_View_Helper_ServerUrl();
// get the ServerUrl
$serverUrl = $serverUrl_helper->serverUrl();
Another approach is to use the ContextSwitch or AjaxContext action-helpers. This allows you to use a view-script from which you can then call your view-helper in the standard manner.
Just use action helpers, many of view helpers are available as action helpers too.
Or directly by using Zend_Date or sprintf.