Yii generate errors from parent controller - yii

I just started using Yii, coming from Codeigniter (CI). I'm trying to set up a situation where the application will check a users credentials before they can even access the site. In CI, I would create a parent controller, put my checks in there, and then inherit that parent controller. I've been trying to do the same thing with Yii, but with no luck.
I first created an init() method (for some reason I can't put the code in a __construct() method). I then perform my check, which works fine. I can throw a new CHttpException, but that looks ugly. How do I use Yii's built in error handling to accomplish what I want to do?

For simple login rules you should just implement the 'accessControl' filters from Yii see Yii documentation on authorizations.
Another way would be to throw the Exception like you already did, and write custom Http error views, place it in the yerfolder/protected/views/system/ (see the yii/framework/views directory for examples)

I solved this by sending the error to the site/error view and then calling Yii::app()->end() to keep the child controllers from loading.

Related

adding more functions in refinery controller and getting error

I am new Refinerycms. I create my own engine called "foodfind" (FoodFinder\vendor\extensions\foodfinds). to become flexible I want to add more functions in client side controller (app\controllers\refinery\foodfinds\foodfinds_controller.rb) and their view pages.
I am trying to do this but getting an error No route matches {:controller=>"refinery/foodfinds/foodfinds", :action=>"new", :locale=>:en}
help
Use rake routes to check if you have a route for that action. If not, then you have to add it to the routes file.

Creating a new view results in 404 Page

I am trying to do something very simple and I seem to be missing something. I tried to scour the internet for results but haven't gotten anywhere so I was wondering if someone can please advise on this seemingly easy and straightforward task.
I have a working MVC Application and have created Models, Controllers, Views using the defaults (scaffolding).
Now I want to create a new view for one of my controller actions:
public ActionResult Index()
{
return View(db.Blog.ToList());
}
So I right click on Action Result and click Add View.
This gives me a dialog box where I specify a view name of "Test", I click "Create a Strongly Typed View" check box and select model class of Blog.
For scaffold template, I leave empty (note I have tried index without any good result)
Now I click the Add button.
As expected this creates a new view test.cshtml under Views/Blogs
Now when I begin without debugging and go to url: localhost:12341/Blog/Test
I get the following error:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Blog/test
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
There could be a lot of issues with why it doesn't work. It is probably worth your while to look into ASP.NET MVC routing. For now, Mystere Man's solution might be a "quick fix" assuming you have everything else set up to their defaults.
In particular, when you tell your browser to go to localhost:12341/Blog/Test then it will (probably) look for a Controller called BlogController and then perform the Test action. In your case, your action is called Index so you would want to go to localhost:12341/Blog/Index (though you may be able to omit Index since it's the default action). Lastly, since your action is called Index then the View() function will automatically look for Index.cshtml. This is detailed somewhat in the msdn documentation:
http://msdn.microsoft.com/en-us/library/dd492930(v=vs.100).aspx
In particular:
If the ViewName property is empty, the current action name is used in place of the ViewName property.
Your action method is called Index, not Test. If you want the url to be /Test, then you need to name the action method Test (there are other ways to do it, but this is the best way)
If you want to use the view Test, then you need to specify it in your View() method.
return View("Test", db.Blogs.ToList());
However, you will still need to use the Index url /Blogs/Index because your action method is Index.

Yii-User and Yii-eauth integration

I am trying to put together an application using yii-user and yii-eauth extensions but I am coming up short. When I create a new webapp and install eauth I can get it to work fine so I know that I am not doing anything wrong on that end. I think the problem lies with my urls. This is a demo of what it is supposed to be like: http://nodge.ru/yii-eauth/demo/login. When someone clicks on say google it should bring you to the google sign in page but on my application I am getting a 404 error that states "The system is unable to find the requested action "login"." The url for this is user/user/login/service/google_oauth whereas the url should read user/login/service/google_oauth. Upon typing this into the browser manually I am redirected to the right page.
So I took a look into the EAuthWidget.php class to see if I could find out where it was getting that extra "user" from but I could not figure it out. I think it may have something to do with the fact that I have this in the user module which is in the /modules/user directory in my webapp. I also tried to use the URLManager to point to the right address but with no luck.
Does anyone have any similar experiences setting this up? Or any suggestions?
You just need to change the widget initialization code in your view(namely change the action property of the widget), somewhat like this:
<h2>Do you already have an account on one of these sites? Click the logo to log in with it here:</h2>
<?php
$this->widget('ext.eauth.EAuthWidget', array('action' => 'login'));
?>
Just to add, keep in mind that this action depends on which view you are including this widget, if the view is protected/views/site/login.php (which is yii's default site login view) then action should be able to go to the yii-user module's login action, so it'll be : 'action'=>'/user/login' , however if you are including this widget in yii-user's protected/modules/user/views/user/login.php then the action will be 'login' as already mentioned.

yii access control filter rules in controllers

When writing rules for access control in yii controllers, possible parameters to be set
for a rule are 'action' - sets to which action the rule applies; 'users', 'roles', etc.
Now, both the yii guide (pdf) and reference (I have chm file) say that it's possible to set,
also, a controller id for the controller the rule should apply to.
Now, if we are already putting these rules in a controller class/file, how would we be able
to put some other controller (other than the current one) as the parameter here, meaning
how would some other controller whose id we mention here - how would it know there is a rule that applies to it, since it's written in a completely other controller class/file?
How would the controller be aware of a rule that mentions it, if it's written outside of it,
in a completely different controller?
You can hook into CWebApplication::beforeControllerAction() to apply filters before the controller even gets the request.
~thinkt4nk
One case where you could use this is if you set some rules in a base controller and extend it. Maybe you have some admin-only controllers, then you can save a little code by just adding these rules into a base Controller that all other controllers extend?
Also, this might be used if you are attaching Behaviors to the controller?
I've never used this rule though, I'm just hypothesizing. :)
Dear Friend,
Yii give us 3 type of user groups
(*)- for All(guest),
(#)- for registered,
(admin)- for super user we can use it from Controller / public function accessRules()
add if you wanna custom user rights than u can also use
1)ttp://www.yiiframework.com/extension/yii-user-management/
and u can also use "Yii-Rights" which is best for customization
2)http://www.yiiframework.com/forum/index.php?/topic/10556-extension-rights/page_p_51869#entry51869
Regard,
Bhavik Chauhan

Z_Form :: Adding a custom error message to zend_form inside Action Controller

I'm new to ZF and I'm discovering how to use Zend_Form and utilize it's capability like validating and filtering input values. I already know the basic of Zend_form like building a form and add element into it. My problem is that I want to add a custom error message to form element and I want to define that message inside the action controller that instantiated the form. I want to defined the error message inside the controller because I need to perform a validation against the database. For example checking if the username/email already exist in the database. I tried googling and that's leads me to setErrorMessage method of zend_form but when I try to use it, the error message is not showing at all... I also tried zend_form->setError and still no error displaying in the view script. Is my idea of setting custom error in the action controller correct or this should be done the other way?
Are you using Zend_Validate_Db_RecordExists?
Something like this should work:
$form->getElement('username')->getValidator("RecordExists")->setMessage('This username exists',
Zend_Validate_Db_RecordExists::ERROR_RECORD_FOUND
);
This works for me:
$form->getElement('username')->addError('This username exists.');