Jump to content
Larry Ullman's Book Forums

KeepLearning

Members
  • Posts

    54
  • Joined

  • Last visited

Posts posted by KeepLearning

  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. 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
  7. 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?
     
     
  8. 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?

  9. 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.

  10. 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.
  11. Your answer this time really cleared up this question for me. Thanks.

     

    Actually, I'm amazed that you are so responsive. On the other hand, customer feedback can help any business improve their product. Your product is your book, and I suppose your reader feedback helps you polish your book. It's a great idea, if you have the time and motivation.

     

    By the way, I do appreciate the other answers you have provided -- even those that I did not thank you for specifically. I don't want to clutter the forum with posts that say "Thanks", but have no other value. Anyway, I do read and appreciate your answers to my questions.

     

    Thanks again.

  12. The filters() method declares what functions should be called before actions are executed (if any). So just listing a function within filters() tells the controller to execute the associated functions.  

     

    Larry, I think you are saying that in this code:

     

     

    "public function myFunctionName() {
    return array('accessControl',"

     

    the "accessControl" part tells Yii that this function is calling a filter (from page 142). The function could be named anything, such as "myFunctionName". It does not need to be named "filters()".

     

    The next part:

     

     

    ", 'httpsOnly + account',);
    }"
     
    tells Yii that httpsOnly must be applied to the account action.
     
    Is this correct?
     
    By the way, why is there a comma after 'account'?
  13. Larry,

     

    I think you are saying:

     

    The following code dictates that the httpsOnly filter should be applied to the "account" action:

     

     

    public function filters() {
    return array(
    'accessControl',
    // account must be accessed over HTTPS:
    'httpsOnly + account',
    );
    }
     
    The above code calls the following function, which verifies there is a secure connection before running $fc:
     
    public function filterHttpsOnly($fc) {
    if (Yii::app()->getRequest()->getIsSecureConnection()) {
    $fc->run();
    } else {
    throw new CHttpException(400,
    'This page needs to be accessed securely.');
    }
    }
     
    I guess $fc is the array returned by the previous function:
     
    array('accessControl', 'httpsOnly + account',);
     
    But how can you "run" an array?
  14. Version 0.5

     

    page 157

     

    Quote:

    "# protected/controllers/SiteController.php

    public function actions() {
    return array(
    'page' => array('class' => 'CViewAction',
    'defaultView' => 'about')
    );
    }
    To change the layout used to encase the view, assign the alternative layout name to

    the layout attribute in that array."

     

    Larry,

     

    I understand the example, but not the last sentence. It doesn't seem to relate to the example. Can you please explain it in other words?

     

    Thanks. 

     

  15. Larry, from what you have written, I understand that protected/views/user/create.php is being rendered by an instance of UserController. That instance of UserController is the current object. It is referred to by $this -- even within the view file. 

     

    However, I do not know what general rule would tell me that the UserController instance is the current object.

     

    If while coding at some future time, I am not sure what the current object is, I suppose I could type "echo get_class($this);". Does that make sense?

  16. I think I understand, but to be sure:

     

    $this UserController" uses the term $this, whereas "$model User" does not.

     

    The distinction is that "$this User" might mean "the current user accessing the site", whereas "$model User" means the instance of $model that contains the currently selected user.

     

    Have I gotten it right yet?

     

    I get the impression this is only an informal documentation syntax, without clearly defined rules. Is this true?
  17. I think you are saying 

     

    1. "$this UserController" is a variable representing "the current instance of UserController"

    2. "$model User" is a variable representing "the current user in the current model"

     

    Am I on the right track?

     

    Thanks.

     

     

     

  18. version 0.5

     

    Page 154 - 155

     

    Larry,

     

    1. What exactly is is a filter chain?

     

    2. In the particular example given, does "invoking its run() method" mean invoking the "account" action's run method?

     

    3. Where can I find the "account" action? Is it in the yii_cms? Should I search for "actionAccount" as a string within the files in the Protected folder?

     

    4. If the "Account" action is hypothetical, would it really have a method called "run()"? If not, what is a run() method?

     

     

    Quoting from the book"

     

    "The method needs to take one parameter, which will be a filter chain. This variable will be used to allow the action to take place by invoking its run() method (i.e., continue the filtering and such)."

     

    The example given is:

     

     

    public function filterHttpsOnly($fc) {
    if (Yii::app()->getRequest()->getIsSecureConnection()) {
    $fc->run();
    } else {
    throw new CHttpException(400,
    'This page needs to be accessed securely.');
    }
    }
     
     
    public function filters() {
    return array(
    'accessControl',
    // account must be accessed over HTTPS:
    'httpsOnly + account',
    );
    }
     
     

    Thanks.

×
×
  • Create New...