Is there a quick, easy method to check, if view exists?
I’m doing something like that in my widget:
if(isset($item['view']) && $item['view'] != '')
echo($this->controller->renderPartial($item['view'], NULL, true));
echo('Error: Property mode is set to view, but no view (property view) is provided!');
Code properly reacts on user-specified view being empty or not set, but fails with exception, when provided view file is not existing. I want to avoid that. How can I do this?
I was wondering if there is Yii built-in function for that? But from your answer it seems, that not.
I’m writing a widget, not an application. I’m not listing available views, I’m only trying to use view provided by user in widget’s configuration. This is totally additional element (not main part of how this widget would work) so I was rather asking like to feed my curiosity. I could leave it even as it is right, so exception would pop up in situation of providing not existing views.
On the other hand, it would be an interesting learn to see, how you would implements listing of available views. If you have some time to spare, share it with us, please.
OK, I got you wrong… when you wrote "user specific view" I was thinking about the customer using the application…
as you explained you are thinking on other developers that will configure the "view"… in this case (IMO) there is no need to check… let the exception fire… as those are developers… and our job is not to do their job… and to repair to their errors… when a developer sees an exception… he knows (should know) what is wrong and he will repair the problem…
to show available views… there is no automatic for that… as you can have many not intended views like partial ones… so you would just manualy compile a list of available views and put a dropdown list for a customer to select one…
Haven’t checked, but if there is a native PHP function that does the job… why would we need a Yii version?
Because, it is always good to have a good Yii function for everything! :] Kidding… I was just thinking, that if there is a rendering function, there should also be a function for checking if view exists. But then again, as you explained, most of the cases views directly are being used by developers not, application users, therefore there is no need for such function, as most of the time view will be permanently, not dynamically selected.
As mdomba told you, you’re trying to open URL resource with function mainly designed to open files in local file system. This is possible and allowed, but not always!
Check, if your PHP configuration (php.ini) and server configuration allow to use open wrappers, that is if you can pass some protocols (like HTTP in your example) to functions normally operating on files. If your configuration doesn’t allows this (as in default configuration in many ISPs and in many server packages - as this is considered as being rather huge security breach) you will always get result coresponding to file not being found, if trying to use wrappers instead of true files.
file_get_contents() is actually trying to open file (load it’s contents to the memory) if it exists, so in a matter of speed of execution and application performance, I would rather use your solution as one of last, when other would fail. Using functions that only checks, if file exists I would personally consider as better - again, only in a matter of performance.
Of course, only if we are talking about checking if file exists and openining it with another function (like view rendering in this example). If we want to check if file exists and open it, if it exists, in the same time, your solution is probably the best for doing so.
I also wanted to notice fouss that his signature is a bit extra-ordinary! :] But, after all - it is up to the user, what he or she has in the signature. For example I’m yelling around that I wrote a lot of posts and wiki articles, which can be seen as rude by someone! :] :] :]
Beside that fouss’ signature has type - should be “click”, not “Clic”! :] “Clic” is in French, if I’m not mistaken! :]
The fastest way to check if a remote file exists is to use headers…
Just recently I had this need, and used a small class for this:
To use it you can do something like
echo "possible network problem, wrong URL, or no internet connection";
else if ("OK"==substr($status,-2))
echo "file exists";
echo "file does not exist";