Getting View Name

Is the View name saved in the controller class or somewhere else where I can get at it while rendering the view itself? I think it would not be much trouble to add a new variable in the controller to get at it in the view, but figured it might be worth a check to see if it’s something built in. I didn’t see anything in the classes I looked in that would ever save when you call the render() method, but who knows ;)

Thanks

Sandy

What do you mean by the "view name"?

If you mean the name of the file, you should be able to use the PHP FILE constant to get the path. You can extract various parts of the path using basename() or pathinfo().

Looking to get the name of the called view to render, for example in the controllers action handler for processing pages I call -


$this->render($view, array('model'=>$model));

The problem is that for google analytics I want to put the ga code in to the header part of the template. But in doing that I need to make sure that my custom code that sets the page name is passing that to ga.

I’m guessing it’s as simple as this -




$this->render($view, array('model'=>$model, 'viewName'=>$view));  // then I can access this when the template, is rendered and get the name.



Looking at the filename might work, but since the code for the header part of the page comes from the template the file name from FILE would, I’m guessing always be the same. (will have to try it).

Keith - thanks for the reply I now have a couple more things to try!

Sandy

Well some testing -

This doesn’t work as the template does not get the variables like a view it seems.

Also the FILE variable does exactly what I thought, it displays the template file name always (main.php) so it’s a no-go.

So back to the original idea -

In the controller I created a member variable called -

$viewName then in the action method that handles all the dispatching of the pages I

$viewName=$view; // $view is always set to the view to render

and finally in the main.php template

$this->viewName is usable with the name of the view.

If that all makes sense, that’s how I did it. Now I can pass the viewName to the google analytics as the page name for tracking and still keep the google code it in the templates header.

Sandy

I’m not sure why you’re passing the view name. What useful information are you getting that can’t be derived from the URL?

In any case, I think it would make more sense to use the controller ID and action ID. You could do that from anywhere with Yii::app()->controller->id and Yii::app()->controller->action->id.

This is the rub to using the URL, this is only one page from the URL perspective, essentially the site is as a multi-page form. So the incoming URL does not change as the user progresses so the easy use of the URL won’t be possible.

If I put Google Analytics in the <head> block it then comes in from the main.php template. However for my google tracking I set specifically the page name as either Landing, Quote, or Confirmation depending on where they are at in the flow of the site.

The code for the <head> </head> block is contained in the main.php template an as mentioned it doesn’t seem to have access to a views passed in render() variables.

Previously I had the google script within each page with the proper view name being sent as the page and page label the user is on. In consolidating the scripts to a single place (in the head block) now I can’t do that and must set the page name based on something else.

The controller ID and action ID’s do not change. The way the multi-page form works is that only one Action handles the request and just changes the rendered view. So the action id would always be the same and I’m guessing since I only have one controller the same as well.

In my main.php (template) in the <head> block I have this code




<script type="text/javascript">

  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){

  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)

  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-abcdefghi-1', 'abcdefghi.com');

  ga('require', 'displayfeatures');  

  ga('send', 'pageview' , {'page':'<?php echo $this->viewName; ?>', 'title':'<?php echo $this->viewName; ?>'});

</script>



What the optional parameters allow in ga send is to set the name of the page and the label that ga will use when I’m looking at a report. So in the report I will see the 3 pages as Landing, Quote, and Confirmation not just the root URL which does not change.

Hope this makes sense???

Sandy