开发者

Code Igniter Passing Variable to Controller Using URL Error

开发者 https://www.devze.com 2022-12-19 05:22 出处:网络
I am creatting a Code Igniter project in which I want to p开发者_JAVA技巧ass a variable through the URL like a get statement, like this:

I am creatting a Code Igniter project in which I want to p开发者_JAVA技巧ass a variable through the URL like a get statement, like this:

url: /site/cake/1

controller function: cake($var)

but when the variable is left blank, I receive an error, how can I get code igniter, to ignore this?


In your controller, do this:

function cake($var = null) {
    // your other code here
}

When $var isn't present in the URL, it will be set to null and you'll receive no error.


To explain why Colin's answer works: The issue you had, was that there was no default value for that controller function. In php, creating a default value for a function parameter is done by assigning it a value in the function definition ($var = false). Now when the cake() function is called with no parameter, it will set $var to false by default.

0

精彩评论

暂无评论...
验证码 换一张
取 消