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