Zend scripts not setup correctly I think - php

When Ive created a new controller, ie in this case Authenticate, Ive also created the folder and file application/views/scripts/authentication/index.phtml
Not a problem when hitting the url http://dev.local/authentication/ but when calling any action ie http://dev.local/authentication/login, I get the error below.
Message: script 'authentication/login.phtml' not found in path (C:\Sites\application\views\scripts\)
Regardless of any changes Im going to make to the login action it shouldnt automatically ask for a new page right? or am I wrong?

By default, each action requires its corresponding view (phtml page). If you want to disable a view/layout for a given action, you can use the following code :
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
EDIT in response to the comment:
I usually don't need to do this, because the actions I have that don't need a view script are redirected/forwared to other actions. For example, once your user is authenticated (i.e. when /authentication/login succeeds), you can redirect him to the home page (or whatever page he was trying to access. Similarly, if the login failed, I simply set an error message in the view and forward to the action that shows the login form.
The only actions for which I use the above code is for actions that are typically called using AJAX and that output some JSON code, for example.

Related

How to access route after certain controller call

I have two routes
Route::post('checkout','CartController#billingSave');
Route::get('success','CartController#billingSave');
So after completing the billingSave method it will redirect to success page..
But if put /success also in url it shows the success page also..though he didn't complete the payment..is there anyway i could let access only the success url after completing the billingSave functionality?
means he can't access success url instead of completing the billingSave method ..or like don't give him access the success url?
You could block the success route using laravel's Middleware https://laravel.com/docs/5.4/middleware You can write your own middleware to check whether billingSave was done.
Change the following route:
Route::get('success','CartController#billingSave');
to
Route::post('success','CartController#billingSave');
so that user cannot access it directly by using the success route in browser, don't forget to show error page here. It can only be accessed by form post method. After data saved successfully submit the form to success route.

How to remember which URL the user was accessing before registration

I am developing a Laravel application and I need some suggestion.
I have a page which when a user who is not logged in access, it redirects to the login page and after authentication, he is back to the same. This works fine.
The problem is that if the user was a new user. He doesnot have a login and goes for Registration.
He then registers and the application redirects him to several other pages (like e.g.He needs to verify his email) and when all is done he will be on the dashboard page.
Now is there any way, I can save that he was on certain page and after registering and moving through all those pages come back to the same page?
Thanks,
Santhosh
Since you haven't posted any code I'd write a general explanation of how I think you should handle this.
In short - you can get the indented url and add it to the registration button as a query parameter i.e yourdomain.com/register?origin=some_route.
So assuming you have a register button/link on your login page, add the origin to the link href:
<a href="register?origin=some_route">Register<a>
This way, when you finish the registration, you can simply access the origin by using \Input::get('origin').
Now, to actually get the intended url you can either try and get it from the Session by using:
\Session::get('url.intended', url('/'))
or you could use \Redirect::intended(url('/'))->getTargetUrl();
In both cases url('/') is used as a fallback url to the homepage and you could replace it with any other url you wish.
Hope this helps!
I imagine you could do something like this for one page:
return redirect()->intended('/dashboard');
which would the the same as
return \Redirect::intended('/dashboard');
As per docs: https://laravel.com/docs/5.2/authentication#authenticating-users
The intended method on the redirector will redirect the user to the URL they were attempting to access before being caught by the authentication filter. A fallback URI may be given to this method in case the intended destination is not available.
Alternately, if you're going through a multi-page login 'wizard' type deal, I'd personally store the initial value of redirect()->intended in a Session or Cookie, and redirect to the value of the session / cookie once your registration process is complete.
Instead of intended redirect function you can print the HTTP referer in a hidden field in the form, then after login redirect to that URL.
<input type="hidden" name="redirect" value="{{URL::previous()}}">
Controller:
if(Input::get('redirect') && (Input::get('redirect') != url('/path/to/login/'))){
return redirect(Input::get('redirect'));
}
else{
return redirect('/alternative/page');
}

Zend, How to access current route from within Action Helper

In Zend Framework I have an Action Helper that loads a login form on most pages. This happens in the preDispatch() method of the Helper and I want to setAction() on the form so that it posts back to the current URL.
What's the best way to access the current URL / route from within the Action Helper? Access the Request (via the Action Controller), then pull then getActionName() and getControllerName(), and concatenate them with baseURL()?
Is there a simpler way? (Set action requires the URI string as a parameter).
Thanks!
You can do as #Elie suggested. However, if you want to use ZF methods for this, you can have a look at this:
$request = Zend_Controller_Front::getInstance()->getRequest();
echo $request->getHeader('referer'); // referer's address
echo $request->getRequestUri(); // current address
I found that I didn't need to access the current URL / route from within the Action Helper. By leaving the form action blank, it automatically posts to the current URL. Perfect.
If I understand correctly, when the user logs in, you want to send them back to the page where they came from. The code I use to do this is:
// the user has come from a particular page - send them back
if($_SERVER['HTTP_REFERER']) {
$this->_redirect($_SERVER['HTTP_REFERER']);
} else {
// the user has come from the home page, or this page
$this->_redirect('/');
}
which is located in the login action (i.e. LoginController->loginAction()).

Wrong return url in Yii framework

I have used Yii::app()->user->returnUrl but it always redirect me to localhost/index.php. Is there any particular configuration or some pieces of code that other programs that I must write? If you have another solutions let me know it.
#aslingga, can you please explain what you're trying to do with returnUrl? Are you just trying to get back to where you were after logging in, or are you using it somewhere else?
This is from the Yii documentation:
Redirects the user browser to the
login page. Before the redirection,
the current URL (if it's not an AJAX
url) will be kept in returnUrl so that
the user browser may be redirected
back to the current page after
successful login. Make sure you set
loginUrl so that the user browser can
be redirected to the specified login
URL after calling this method. After
calling this method, the current
request processing will be terminated.
In other words, if the page you're trying to request requires authentication, the URI of the page you're on gets stored in a session variable. Then, once you've logged in, it takes you back to that page.
One way I'd recommend troubleshooting is to do a print_r($_SESSION); just to make sure the returnUrl is actually being stored. Then you'll be able to check if index.php is being stored as returnUrl or if you're just being redirected there for some reason.
Looking at the CWebUser methods getState and setState might also be helpful.
I know this question is old but maybe this will help someone out since I didn't couldn't find a decent answer anywhere.
How getReturnUrl works
Setting a default return URL for your Yii app requires a bit of customization. The way it works out of the box is that you specify the default return URL each time you call it:
Yii::app()->user->getReturnUrl('site/internal');
The idea being that if a user were to visit a page that requires authentication, they will get redirected to the login page, but not before the site running
Yii::app()->user->setReturnUrl('site/visitedpage');
Now when the user logs in, they will be returned to the page they intended to go to.
While I like that functionality, having to set the default return URL each time is dumb. If you want to change the default return URL, you have to go find it throughout your code. I suppose you could set the value in a site parameter and call
Yii::app()->user->getReturnUrl(Yii::app()->params['defaultReturnUrl']);
I don't think I have to explain why that solution is annoying too.
My Solution
So when getReturnUrl is called without any parameters, it returns either '/index.php' or just '/'. This is fine in some cases, but not always. This is better IMO.
First, extend the CWebUser class and add the following extras
class WebUser extends CWebUser {
// default return URL property
public defaultReturnUrl;
// override the getReturnUrl method
public function getReturnUrl($defaultUrl=NULL) {
if ($defaultUrl === NULL) {
$defaultReturnUrl = $this->defaultReturnUrl;
}
else {
$defaultReturnUrl = CHtml::normalizeUrl($defaultUrl);
}
return $this->getState('__returnUrl',$defaultReturnUrl);
}
}
Now, let's add a couple items to the user component array.
'user' => array(
'class' => 'WebUser',
'defaultReturnUrl' => 'site/internal'
)
Not only does this allow you to set a default return URL in the config, but also maintains the ability to set a different default return URL and use the setReturnUrl functionality.
I think, you must set it:
Yii::app()->user->setReturnUrl('controller/action');

Redirect after Post

After form (method POST) submission i want redirect user to specific page.
usually i used simple line
header("Location: /path/to/redirect/");
exit;
The Zend_Controller_Action have method _redirect example:
$this->_redirect("/path/to/redirect/");
But it have one simple problem: if i refresh page (press F5) last controller action is activated. So its like double post.
Of course i can use old fashion way, but I just want find the zend style redirect.
Edit: p.s after post redirect i want have cleaned form data. Of course i can use own method with header("location:/path") but I searching it implemented in standart zf
Any ideas?
I think thats because _redirect uses an internal redirect. You need to use an external one. You need to use the Redirector action helper directly... in your action:
$this->_redirector->gotoUrlAndExit($url);
Set a session variable that data has been posted, if not post data, redirect?

Categories