Trying to get property of non-object_thinkphp Route error

Source: Internet
Author: User
Tags echo date

While reading the thinkphp5.0 QuickStart, this section of the Model Foundation encounters a bit of a routing problem. Take a note of this essay.

The problem encountered:

[8] errorexception in usercontroller.php LineTrying to get property of Non-object

controller.php

Public function Read ($id = ") {
$user = Usermodel::get ($id);
echo "-----------function read () running-----------<br/>";
echo "ID:". $id. " <br/> ";
echo $user->nickname. ' <br/> ';
echo $user->email. ' <br/> ';
echo Date (' y/m/d ', $user->birthday). ' <br/> ';
}
Public Function read2 ($id = ") {
$user = Usermodel::get ($id);
echo "-----------function read2 () running-----------<br/>";
echo "ID:". $id. " <br/> ";
echo $user [' nickname ']. " <br/> ";
echo $user [' email ']. <br/> ";
echo Date (' y/m/d ', $user [' Birthday ']). " <br/> ";
}

This problem is encountered because a routing rule is added that is not the same as the tutorial. A rule is defined in the tutorial:

' User/:id ' = ' index/user/read ',

This allows access to the Read method when the browser enters HTTP://WWW.MJTEST.COM/USER/3.

I have added a new read2 method behind the Read method, in order to access this method, a new one is added after the routing rule:

' User/read2/:id ' = ' index/user/read2 '

In other words, the last two lines of routing rules are now

' User/:id ' = ' index/user/read ',
' User/read2/:id ' = ' index/user/read2 ',

In this way, the browser input HTTP://WWW.MJTEST.COM/USER/READ2/3, and did not as expected to access the Read2 method, In fact, this URL in order to read the routing rules,

Read the ' user/:id ' + ' index/user/read ' rule first, and think the URL conforms to this rule. I passed the read2,3 as two parameters to the Read method

Browser input Results

The result is that the server actually accesses the Read method.

Modify the route as follows:

route.php
' User/read2/:id ' = ' index/user/read2 ',
' User/:id ' = ' index/user/read ',

Visit URL:HTTP://WWW.MJTEST.COM/USER/READ2/3

Access results:

-----------function read2 () running ...-----------
Id:3
Unease
[Email protected]
1977/03/05

This gives you the right access to the Read2 method.

Trying to get property of non-object_thinkphp Route error

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.