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

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');

Related

Working with CodeIgniter routes?

I think this is a route issue but I'm not sure. I have a page with this URL:
siteurl.com/kowmanger/titles/titles/edit/$id
I'm trying to find out that when I'm on this page I load the titles page it says page not found so I need to tell it that the $id is just a paramter so I can use it to get the data of the title.
UPDATE :
So I decided to change my titles controller so that there's a edit and add function inside of the titles controller that way they dont' have separate controllers when they are in fact methods.
So now I have:
kansasoutalwwrestling.com/kowmanager/titles/titles - list of titles
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
kansasoutalwwrestling.com/kowmanager/titles/titles/edit/$id - edit form
I don't have any routes set up so far for this. For some reason though I"m getting the same page for both of these page.
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
(right link url) kansasoutalwwrestling.com/kowmanager/titles/add -
addnew form
I need a route so that it'll show the correct url if the add method is accessed.
Also I need to set up a route so that if the correct edit link is accessed it sees the id attached to the end of the url and it'll accept it so that I can do a my database query to get the title data.
UPDATE: So to reiterate I have a module(subfolder) called titles. Inside of the module I have a controller called titles and inside of that controller I have 3 functions called index(), add(), edit().
I tried using Chris's suggestion on the routes but its not routing correctly. Also wanted to mention I'm using wiredesignz modular separation framework if that matters.
Any additional ideas?
Possible answer based on your post, not one hundred percent your entire structure but if i had to guess based off the post I would try this as my routes first..
$route['titles/titles/edit/(:any)'] = 'titles/titles/edit/$1';
$route['titles/titles/add'] = 'titles/titles/add';
$route['titles/titles'] = 'titles/titles';
$route['titles'] = 'titles/index';
Are you using custom routing in your configuration files ?
The general routing protocol used by codeigniter is like this:
domain.com/controller/methode/param1/param2/param3
This being said, your url
siteurl.com/kowmanger/titles/titles/edit/$id
corresponds to something like this :
class Kownmanger extends CI_Controller
{
public function titles($titles, $action, $id)
{
}
}
In case you are using sub-folders in your controllers folder, what I have just said will change, Could you please tell us what's your directory structure ?

How to check if Controller already exists when already configured route using Codeigniter?

On my website, I am loading the content dynamically from database like this
e.g mysite.com/about-us
for this, there is an enrtry in database, so it will load the content for 'about-us' & print it using "page" controller only.
for this what I have done is, I have added below configuration in routes.php
$route[':any'] = "page";
but lets say if I already have controller named "about-us" and I want to load that & not the one from database, how can I do that?
A smooth solution would be to use the error/missing_page controller and point it in the config/routes.php.
Then it would automaticly pick all existing controllers first, and then that controller.
You can also call show_404() if you don't find a record in the database.
This allows you to create new controllers without having to point all of them in the route file.
Read about 404 override here
you need to add this
$route['about-us'] = "aboutus";
$route['about-us/(:any)'] = "aboutus/$1";
before
$route[':any'] = "page";
as the CI route is not greedy, it will not check for the page controller after it finds the about-us controller.

Getting Current Controller ID in Yii

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)

how to register new view in compoenent

I'm using Joomla ads_manager. My question is how can I register new view in the component? I ad a folder put the same code as other view for example rules that is only text (the rules component works) and it gave me
500 - View class not found [class, file]: adsmanagerViewregister, C:\xampp\htdocs\trademac-php\trunk\components\com_adsmanager\views\register\view.html.php
error
What is the problem, I've registered the view in the router of the component. that is working it is redirecting to the register bu the error appears.
You will also need to change the code , just adding the folder is not enough.
class X*View*Yy extends JView
where X Is ur component name and Yy is ur view name (note the first caps letter in the view name)
you also need to have a file called default.php inside the viewfolder
VIEW-Name
tmpl/default.php
view.html.php

Inserting index.phtml view from side controller in the main index.phtml

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

Categories