Get all the "pages" in YII?

I am trying to create my own XML sitemap. Everything is done, except for the part that, as I thought, would be the simplest. How do you get a list of all the pages on a site? I have a bunch of views in a folder / site and several others. Is there a way to explicitly request their URLs, or perhaps via controllers?

I do not want to use the extension

+8
php yii sitemap
source share
5 answers

You can use reflection to repeat all the methods of all your controllers:

Yii::import('application.controllers.*'); $urls = array(); $directory = Yii::getPathOfAlias('application.controllers'); $iterator = new DirectoryIterator($directory); foreach ($iterator as $fileinfo) { if ($fileinfo->isFile() and $fileinfo->getExtension() == 'php') { $className = substr($fileinfo->getFilename(), 0, -4); //strip extension $class = new ReflectionClass($className); foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { $methodName = $method->getName(); //only take methods that begin with 'action', but skip actions() method if (strpos($methodName, 'action') === 0 and $methodName != 'actions') { $controller = lcfirst(substr($className, 0, strrpos($className, 'Controller'))); $action = lcfirst(substr($methodName, 6)); $urls[] = Yii::app()->createAbsoluteUrl("$controller/$action"); } } } } 
+9
source share

You need to know what content you want to include in your sitemap.xml file, I really don't think you want to include ALL pages in the sitemap.xml file, or you really want to include something like site.com/article/edit/ one?

However, you can get the result of the view action in your controllers. True, you need to know what you want to index.

Do not think in terms of controllers / actions / views, but rather think about the resources of your system that you want to index, be it articles or pages, they are all in your database or stored somehow, so you can list them, and they there is a URI that identifies them; getting a URI is a function call of a pair.

+1
source share

There are two possibilities -

Case 1:

You start a static website, then you can find all your HTML inside 1 folder - protected / views / site / pages http://www.yiiframework.com/wiki/22/how-to-display-static-pages-in -yii /

Case 2:

The site is dynamic. Tasks, such as creating and restoring Sitemaps, can be divided into background tasks.

The launch of background ones can be achieved by emulating a browser that is possible on Linux using the WGET , GET, or lynx commands.

Or you can create a CronController as a CConsoleCommand . How to use commands in YII is shown in the link below - http://tariffstreet.com/yii/2012/04/implementing-cron-jobs-with-yii-and-cconsolecommand/

A sitemap is an XML listing the URLs of your site. But he does more than that. This will help you visualize the structure of the website, perhaps

  • category
  • subcategories.

When creating a useful extension The above points can be considered before design.

Frames, such as Wordpress , provide a way to create a categorical site map. Thus, the metadata for each page is stored earlier and using the metadata that it detects and groups the pages.

The reflection solution proposed by @Pavle is good and should be the way to go . Think that there may be partial views , and you may or may not want to specify them as separate links.

So how much effort you want to make to create an extension also depends on some of them.

You can either ask the user to list all the variables in config fie and go from there, which is not bad, or you need to group the pages and list using some methods, such as mirroring and parsing pages and searching for a regular expression.

For ex - based on the names of the modules, you can first group them, and the controllers inside the module can form a subgroup.

+1
source share

One of the first steps may be to iterate over the view files, but then you need to consider that in some cases the views are not the landing pages of the pages, but the sections of the pages included in other pages using CController::renderPartial() . Having studied the CController Class Reference, I came to the CController::actions() method.

So, I did not find any Yii method for iterating over all CController actions, but I used php to iterating over all SiteController methods in one of my projects and filtering them using the prefix action ', which is my action prefix, here is a sample

 class SiteController extends Controller{ public function actionTest(){ echo '<h1>Test Page!</h1></p>'; $methods = get_class_methods(get_class($this)); // The action prefix is strlen('action') = 6 $actionPrefix = 'action'; $reversedActionPrefix = strrev($actionPrefix); $actionPrefixLength = strlen($actionPrefix); foreach ($methods as $index=>$methodName){ //Always unset actions(), since it is not a controller action itself and it has the prefix 'action' if ($methodName==='actions') { unset($methods[$index]); continue; } $reversedMethod = strrev($methodName); /* if the last 6 characters of the reversed substring === 'noitca', * it means that $method Name corresponds to a Controller Action, * otherwise it is an inherited method and must be unset. */ if (substr($reversedMethod, -$actionPrefixLength)!==$reversedActionPrefix){ unset($methods[$index]); } else $methods[$index] = strrev(str_replace($reversedActionPrefix, '', $reversedMethod,$replace=1)); } echo 'Actions '.CHtml::listBox('methods', NULL, $methods); } ... } 

And I got the result.

All the actions in a ListBox!

I am sure that it can be further clarified, but this method should work for any of the controllers that you have ...

So what you need to do:

For each controller: Filter out all class inactivity methods using the method described above. You can create an associative array, for example

 array( 'controllerName1'=>array( 'action1_1', 'action1_2'), 'controllerName2'=>array( 'action2_1', 'action2_2'), ); 

I would add the static getAllActions () method to my SiteController for this.

get_class_methods , get_class , strrev and strlen are all PHP functions.

+1
source share

Based on your question: 1. How do you get a list of all the pages on the site?

Based on the Yii module / controller / action / action_params and your need to create a sitemap for SEO.

  • It will be difficult to parse automatically to get all the URLs, as your action options change indefinitely. Although you could just get the controller / action easily, as built by Pavel Predic. Difficulty arises when you set up URL (SEO) rules for SEO.

The next best solution is to have a content database, and you know how to get each content using URL rules, and then setting the cron console to create all the URLs that you need to save in the sitemap.xml file.

Hope this helps!

+1
source share

All Articles