Getting Current Controller ID in Yii - php

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)

Related

How to get controller name and action name on view in Zend Framework?

I am trying this code:
Zend_Controller_Front::getInstance()->getRequest()->getControllerName()
Zend_Controller_Front::getInstance()->getRequest()->getActionName()
I get this error:
Zend_Controller_Front Class Not Found
I am giving answer on the basis of what i have get from your question it might be wrong but from my point of view I am giving this answer.
$action= $this->getEvent()->getRouteMatch()->getParam('action');
$controller= $this->getEvent()->getRouteMatch()->getParam('controller');
you can write this in controller and pass this variables in view model.
$view = new ViewModel(array('action' =>$action,'controller' =>$controller));
$view->setTemplate("tournamentview/index/getTeam.phtml");
return $view;
and you can access action and controller variable in view i.e. getTeam.phtml file.$action will give you the action Name and the $ controller will give you the alias of controller like 'TournamentView\Controller\Index' hope it will work

codeigniter issue with loading view from controller

I am new to codeigniter and really interested in trying it out. I followed the guide but ran into a problem. It seems like I am unable to load my first page properly.
I have inside the view folder another folder called general and inside it index.php.
in controller folder, I have sub-folder with general and inside it is Default controller.
I have the following route and yet the page showing is blank
$route['default_controller'] = "general/default";
$route['404_override'] = '';
When I visit the link, I type this in browser:
http://localhost:8888/treventa/
and the screen is blank. Am I doing something wrong? Sorry if this is too simple but a person got to learn from his mistake :)
Try with me step by step:
Firstly: the Controller:
(main.php) File content
if (!defined('BASEPATH'))exit('No direct script access allowed');
class Main extends CI_Controller {
public function index() {
$this->load->view('general/welcome');
}
}
Secondly: The view:
(welcome.php) File content
You can put anything you want
<h1> Hello, I'm the view file </h1>
Finaly: The routes:
(routes.php) File content
$route['default_controller'] = "general/main";
Now call the script like this http://localhost/codeIgniter/, that's where codeIgniter is the script folder name.
I think everything now is clear.
CI trying to use method default() of general controller. If You want to use folders for controllers, make sure You specify method, so try $route['default_controller'] = "general/default/index";
And one more thing: this method (default_controller) will be used when You trying reach Your root http://localhost:8888/, if You want to route request from http://localhost:8888/treventa/ to default controller, You need to add route-rule to /config/routes.php something like $route['treventa'] = "general/main/index";

What is required to call a controller in Codeigniter?

I'm creating a simple blog with Codeigniter. But I'm having trouble calling another controller besides the default controller.
The following URL takes me to the default controller as specified in my config/routes.php.
blog/index.php
According to the documentation, simply appending the name of another controller saved in controllers/ is all that is needed:
blog/index.php/blog_login
Here is my controller class, named blog_login.php:
class Blog_login extends CI_Controller {
public function index()
{
echo 'It works!';
}
}
But doing this throws a 404 error, which makes me feel that I'm missing something. Is there something else that I am supposed to configure before trying to access a different controller?
http://codeigniter.com/user_guide/general/routing.html Read this properly, it couldn't be clearer.
According to the documentation, simply appending the name of another
controller saved in controllers/ is all that is needed
This is not true. If you want to call another controller 'Blog_login', you simply put the name of the controller as the first segment of the url:
domain.com/index.php/blog_login
This will never work:
blog/index.php/blog_login
Index.php (unless you remove it via .htaccess) always follows right after your domain.com
Finally, you don't need to specify routes unless you're doing something non standard. So
domain.com/index.php/blog_login - calls the index() function in your Blog_login controller
domain.com/index.php/blog - calls the index() function in your blog controller
domain.com/index.php/blog/search - calls the search() function in your blog controller.
None of the above examples need an entry in routes.php
When u call:
blog/index.php/blog_login
you're really calling a method called "blog_login" in your "blog" controller. If you want to call another controller, it must have the following structure:
controller_name/controller_method
So, if you wanna call your blog_login controller just call it like this:
blog_login/
Note: Sometimes it's necessary to add the base_url() to your URL in order to make CI understand correctly the URL.

Lithium framework: how to obtain controller name in layout

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

how do I render a specific view from an an urelated action?

I need to render the view for controller=user action=profile from a
controller = b action =c
i.e. /b/c will render the same view as when I surf to /user/profile
How can this be achieved (except using include inside the view file) in Yii?
What code do I have to put in the controller?
To render which view is not decided by the controller or action id, you can modify it easily. Just change this line in your b controller c action:
$this->render('[path alias to your user/profile view]',array(
$model=>[your data provider]
));
You can check the manual to find how to make a path alias, here's an example:
application.views.user.profile
You can also use a "root view path" syntax to render any view file by starting with "//" like:
$this->render('//user/profile');

Categories