Jump to content
Larry Ullman's Book Forums


  • Posts

  • Joined

  • Last visited

KeepLearning's Achievements


Newbie (1/14)



  1. Thanks. But following Larry's example, this works for me: 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ '<controller:\w+>/<id:\d+>' => '<controller>/view', '<controller:\w+>/<action:\w+>' => '<controller>/<action>', 'site/page/<view:\w+>'=>'site/page', ] ] The following two URLs now return the correct page: ullman.local/index.php/site/page/testpage1 If there is no htaccess file, then set 'showScriptName' to true, or omit this line altogether. If there is an htaccess file, then showScriptName can be either be true or false, so it's irrelevant. ullman.local/site/page/testpage1 This works only if an htaccess file is present in the web folder. showScriptName can be either be true or false, so it's irrelevant. The way I see it, the order doesn't matter. What made the difference is that Larry's rule starts with "site", as opposed to starting with "page". Also, for both URLs, this line can be omitted entirely: 'showScriptName' => etc Since it can be omitted, I wonder if there is any legitimate purpose for 'showScriptName'. (It's mentioned on p. 66) Anyway, I'd like to suggest that this line be added to the book: 'site/page/<view:\w+>'=>'site/page', If this section of the book were clearer, I would not have had to devote so many hours of my scarce time to understanding it over the past 10 days. Sorry to be blunt, but I hope that feedback is helpful. Thanks.
  2. Thanks for clarifying that. Yes, I did try renaming the "page" folder to "pages", as noted in my previous post, but that did not solve the problem. Actually, I have now deleted the entire project, and re-created it, but still the problem remains. I have documented every step I have taken, as follows: Start in E:\xampp\htdocs and run these: composer self-update composer global update composer create-project --prefer-dist yiisoft/yii2-app-basic ullman Next, modify config/db.php to connect to the database, which I downloaded from the YiiBook2 website. Also, I've previously modified the following files to allow running the site locally: httpd-vhosts.conf hosts Point my browser to ullman.local/ to verify that I can see the home page and the about page, and yes I can. Put an .htaccess file in the ullman/web folder, with the following: RewriteEngine on # If a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Otherwise forward it to index.php RewriteRule . index.php In config/web.php, insert the following code below the db line: 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ '<controller:\w+>/<id:\d+>' => '<controller>/view', '<controller:\w+>/<action:\w+>' => '<controller>/<action>', ] ] Verify that the About page can be accessed at the following URL: http://ullman.local/site/about As per the YiiBook2 instructions, create a pages folder here ("pages" is plural): E:\xampp\htdocs\ullman\views\site\pages Create a file named testpage1.php in the folder, containing the following code: <?php /* @var $this yii\web\View */ use yii\helpers\Html; $this->title = 'About'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="site-about"> <h1><?= Html::encode($this->title) ?></h1> <p> This is the test page for page 161 of the Ullman book. </p> <code><?= __FILE__ ?></code> </div> Insert this line in the SiteController.php Actions method: 'page' => ['class' => 'yii\web\ViewAction'] Point the browser to these two URLs: ullman.local/site/page/view/testpage1 (As per the book) ullman.local/page/testpage1 (As per Brent's suggestion) With pretty URLs enabled, both URLs return a 404 error. With pretty URLs disabled, both URLs return the home page, not the testpage1.php. Add this to config/web.php in the urlManager section, as per Brent's suggestion: 'page/<view:\w+>'=>'site/page', With pretty URLs enabled, both URLs return a 404 error. With pretty URLs disabled, both URLs return the home page. Thus, the following code is still not working: 'page' => ['class' => 'yii\web\ViewAction'] I think I have followed the instructions exactly, but the problem remains. Am I doing something wrong?
  3. Thanks Brent, but it still doesn't work for me. My static page is now located here: E:\xampp\htdocs\ullman\views\site\page\testpage1.php My urlManager looks like this: 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ '<controller:\w+>/<id:\d+>' => '<controller>/view', '<controller:\w+>/<action:\w+>' => '<controller>/<action>', 'page/<view:\w+>'=>'site/page', ] ] And I've pointed my browser to this URL: ullman.local/page/testpage1 But I'm still getting a 404 error. I also tried changing the "page" directory to "pages" to match what you wrote above, but no luck. In other words, I still cannot get this code to work, whether pretty URLs are enabled or not: 'page' => ['class' => 'yii\web\ViewAction'] Larry, have you actually tested your example to see if it works with pretty URLs enabled and disabled? Can you please let me know? Thanks.
  4. Actually, I don't think anything has worked so far. Even when I thought something was working, I was actually seeing the home page, instead of the static "About" page. So I need to start over, using a new static page that is called "testpage1" instead of "About" -- as per Larry's suggestion. Here is the location of my new static page: E:\xampp\htdocs\ullman\views\site\page\view\testpage1.php It contains this code: <?php use yii\helpers\Html; /* @var $this yii\web\View */ $this->title = 'About'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="site-about"> <h1><?= Html::encode($this->title) ?></h1> <p> This is the test page for page 161 of the Ullman book. </p> </div> Here is my "actions" controller in SiteController.php: public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, ], [ 'page' => ['class' => 'yii\web\ViewAction'] ], ]; } Here is the urlManager in web.php: 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ '<controller:\w+>/<id:\d+>' => '<controller>/view', '<controller:\w+>/<action:\w+>' => '<controller>/<action>', ] ] Using all of the above, with pretty urls enabled, all of the following URLs give me a 404 error: ullman.local/site/page/view/testpage1 ullman.local/site/page/testpage1 ullman.local/site/testpage1 ullman.local/testpage1 If I disable pretty urls, like this: 'enablePrettyUrl' => false, then all of the following URLs give me the home page, which displays "Congratulations!": ullman.local/site/page/view/testpage1 ullman.local/site/page/testpage1 ullman.local/site/testpage1 ullman.local/testpage1 What should be displayed is this text: "This is the test page for page 161 of the Ullman book.", which is what's written in testpage1.php. How can I fix this? I've created a default route to show all static pages, right? Testpage1.php is a static page located in the correct folder. So it should be displayed. But it is not. So how can I make this work? Thanks.
  5. Thanks Brent, but I could not get it to work. Here's my URL code, including your suggestion: 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ '<controller:\w+>/<id:\d+>' => '<controller>/view', '<controller:\w+>/<action:\w+>' => '<controller>/<action>', 'page/<view:\w+>'=>'site/page', ] ] Even after adding your code, I still need to disable pretty urls to access this web page: ullman.local/site/page/view/about I want to be able to access the above page even when pretty URLs are enabled. By the way, is "yiistuff" an actual folder, or does it represent something else? Thanks.
  6. Thanks, but I knew that already. The problem is that with this setting: 'enablePrettyUrl' => true, the code in the book does not work. Here is the code that is provided in the book: 'page' => ['class' => 'yii\web\ViewAction'] Is there some way to modify the code in the book to make it work even when pretty URLs are enabled? Thanks.
  7. In my config/web.php file, I have this: 'db' => require(__DIR__ . '/db.php'), 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => true, 'rules' => [ '<controller:\w+>/<id:\d+>' => '<controller>/view', '<controller:\w+>/<action:\w+>' => '<controller>/<action>', ] ] If this is set to true: 'enablePrettyUrl' => true, then this url does not work: ullman.local/index.php/site/page/view/about However, if this is set to false: 'enablePrettyUrl' => false, then the same url does work: ullman.local/index.php/site/page/view/about Why does this URL not work when I enable pretty URLs? [Edited:] If I have an .htaccess file in the web folder, then I get the same result as above for this URL: ullman.local/site/page/view/about In other words, pretty URLs cannot be enabled. But why? Thanks. Note: The .htaccess file contains this: RewriteEngine on # If a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Otherwise forward it to index.php RewriteRule . index.php
  8. Hi Larry. I'd very much appreciate a reply from you about this. Should I expect your reply sometime this week perhaps? Thanks.
  9. I'm not sure how the second part of this example is related to the first, and where it belongs. I am quoting from page 174 of YiiBook2: # models/Page.php public function getUser() { return $this->hasOne(User::className(), ['id' => 'user_id']); } This means you can fetch every page with every page author in one step: $pages = Page::find()->with('user')->all(); I'm guessing that with('user') calls the getUser method. Is that right? But in which file does the second part of the example belong? In the view file?
  10. I've read through the example at the top of page 161 several times now, and I just cannot understand it. So I tried out the code, but I cannot get it to work. Here's what I did: I downloaded the yiibook2_cms.zip file. I installed it as ullman.local, and verified that it works in my browser (using Xampp on Win 7). To be clear, the composer.json file is here: E:\xampp\htdocs\ullman\composer.json. In Sublime Text, I opened SiteController.php, added this code, and saved the file: [ 'page' => ['class' => 'yii\web\ViewAction'] ] As a result, my actions method now looks like this: public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, ], [ 'page' => ['class' => 'yii\web\ViewAction'] ], ]; } Then I created a new "about.php" file in the following folder: E:\xampp\htdocs\ullman\views\site\page\view\about.php I did this by creating 2 new folders (page and view), and by copying and modifying the existing about.php file. (I'm on Windows, which uses backslashes in the path.) Then I pointed my browser to: ullman.local/site/page/view/about This gives me the following 404 error: Object not found! I also got a 404 error when I pointed my browser to: ullman.local/index.php/site/page/view/about However, I do get the usual about page if I go here: ullman.local/index.php/site/about What am I doing wrong?
  11. For pretty urls, something that puzzled me was where to put the .htaccess file, but I realized it goes in the web folder.
  12. When I tried to run the downloaded SQL commands in phpMyAdmin, various error messages appeared, stating that no author table exists. So I searched for all occurrences of the word "author" and replaced them with "user", except for this line: `type` enum('public','author','admin') NOT NULL, Is this correct? The file I downloaded is: yiibook2_cms.sql.zip Thanks.
  13. Larry, Do you have any more chapters of the Yii Book? I mean the second edition, revised for Yii 2. I've read -- and mostly understood -- up to page 190, which is the last revised page. If you have more revised chapters, I'd be interested in reading them. No doubt, other readers would too. Can you post any additional chapters on your website and let everyone know? Thanks.
  14. Larry, on page 150 of the YiiBook2 (latest version), you write that the following code is "verbose, redundant, and illogical": $model = new Page(); $model = $model->findOne($id); Instead of creating an instance of the Page class, as shown above, you say we should use a static class instance (or static method?), like this: if (($model = Page::findOne($id)) !== null) { return $model; etc. I don't understand why the first example is "verbose, redundant, and illogical", and why the second example is preferred. Can you please explain this? Thanks.
  • Create New...