Redirect toUrl does not work (zf2) - php

I have a problem with a redirect in Zend Framework 2.
when a user enters a url like http//mysite.test/lorem, he will be redirected to http//mysite.test/news/view/lorem-ipsum.
the code:
$redirecttable = $sm->get('Redirect\Model\RedirectTable');
$route = $redirecttable->getRedirectByRoute($this->params()->fromRoute('page'));
if($route) {
// $route->getToroute() returns "news/view/Lorem-Ipsum"
return $this->redirect()->toUrl( $route->getToroute() );
}
Am I making a stupid mistake here?

The toUrl method redirect to a complete url, but you're sending part of url. You can use $this->redirect()->toRoute($route,$params) for redirecting to a route. reference
If you want to use toUrl method, try this code:
return $this->redirect()->toUrl( $this->getRequest()->getUri()->toString().$route->getToroute() );

Use toRoute() instead of toUrl():
return $this->redirect()->toRoute($route->getToroute())

Related

Redirect a route with parameters in url to another route with these parameters (Laravel)

I have a url and I want make a redirection and I want to keep the url parameters.
I'm using the ->with method but it doesn't work.
Example:
http://mywebsite.local/product-name-random?select_article=710
I want redirect this url to:
http://mywebsite.local/father-category-of-this-product?select_article=710
I'm using this code but it doesn't work:
return redirect()->route('web.custom_url', $father_cathegory->seo->slug)->with('select_article', Input::get('select_article'));
->with() is not working. Nothing happens.
Im using Laravel 5.5.
You should try this way:
return Redirect::to('/admin/slider?rdStatus='. $rdStatus);
you can also use
redirect()->route('web.custom_url',[ $father_cathegory->seo->slug])->withInput();
it will redirect to route with input
Using ->with() you're flashing the session.
You could do something like this:
$to = route('web.custom_url', $father_cathegory->seo->slug) .
'?select_article=' . Input::get('select_article');
return redirect()->to($to);

How could i keep my $_GET params on laravel Redirect?

I try to redirect by using,
return redirect('/customer/' . $cust_id)->with('page',2);
My Route is,
/customer/{id}
In Customer page, i use pagination.
So i need to redirect to the same page as it comes.
But how could i pass my get param with "redirect()"
Actual URL,
http://domain.com/customer/2?page=3 // page=>3
I need to send back the control to the same page.
Is there any solution ?
Or Is it correct way to approach ?
The native PHP function http_build_query() could be handly if you've got several GET variables that may be differnet for each re-direct.
// check for GET variables and build query string
$get = count($_GET) ? ('?' . http_build_query($_GET)) : '';
// redirect
return redirect('/customer/'.$cust_id.$get));
I don't use Laravel so there may be better ways of doing this but it seems like a clean solution anyway.
You should check if it's set, otherwise not redirect using the item.
Your code will be:
return redirect()->back();
Or with notification you could do:
return redirect()->back()->with('message', 'This is my message!');
You can get parameters with request()
$page = request->has('page') ? '?page='.request()->page : '';
return redirect('/customer/'.$cust_id.$page);
But if you need to redirect back to the previous page, use this:
return redirect()->back();

Codeigniter Controller return to previous page

I'd like to ask you how can I instead of $this->load->view('some_view.php') at the end of controller code, return user to page from where he invoked controller method? Simple return statement is not working.
ie.
public function someMethod($IDCustomer) {
$this->Some_modal->persist($IDCustomer);
// how to return to previous page instead of line after?
// i've used $this->load->view('someView.php');
}
This should help http://www.codeigniter.com/user_guide/libraries/user_agent.html
$this->load->library('user_agent');
if ($this->agent->is_referral())
{
echo $this->agent->referrer();
}
or straight PHP:
redirect($_SERVER['HTTP_REFERER']);
I've found answer on some thread.
In the page that you want to go back to you can do:
$this->session->set_userdata('referred_from', current_url());
Then redirect back to that page
$referred_from = $this->session->userdata('referred_from');
redirect($referred_from, 'refresh');
Tried return redirect()->to($_SERVER['HTTP_REFERER']); , this would work well.
In Codeigniter 4 You can use previous_url() function from url helper
find more https://codeigniter.com/user_guide/helpers/url_helper.html
I've tried header('location:'.$_SERVER['HTTP_REFERER']); and it's working quite well.
Just a one-liner plain old PHP code.
Use the REDIRECT_QUERY_STRING alternative of HTTP_REFERRER:
// set in session redirect back URL in your common is_logged_in function if user is not logged in
$CI->session->set_userdata('redirect_back', $_SERVER['REDIRECT_QUERY_STRING']);
// below code user after successful login in auth.php library
if($this->ci->session->userdata('redirect_back')){
$redirectBackUrl = $this->ci->session->userdata('redirect_back');
$this->ci->session->unset_userdata('redirect_back');
redirect(base_url() . $redirectBackUrl);
}

How to set returnUrl value in Yii

I am using Yii and the problem I am getting is with the Yii::app()->user->returnUrl. It always returns me to the index.php page.
How can I set its value to the page which requested the current page as I do not know from which page user has visited the current page?
You can use Yii::app()->request->urlReferrer to see where the user came from.
public function beforeAction()
{
Yii::app()->user->returnUrl = Yii::app()->request->urlReferrer;
return parent::beforeAction();
}
Be careful, if the user came from a 3rd party site, then this will redirect them away from your site.
For those using Yii2
Yii::$app->user->returnUrl = Yii::$app->request->referrer;
There is no such thing as: Yii::app()->user->urlReferrer
It should be: Yii::app()->request->urlReferrer or Yii::app()->request->requestUri (current page)
So try this:
Yii::app()->user->returnUrl = Yii::app()->user->urlReferrer;
Or this one (which I personally prefer):
Yii::app()->user->returnUrl = Yii::app()->request->requestUri;
I created a Yii extension to manage return URLs, you can find it here:
https://github.com/cornernote/yii-return-url#yii-returnurl
It's an improvement over the way Yii handles it because it stores the return url in the GET/POST data instead of the SESSION. This means that if you have multiple tabs open then each can have it's own return url.
You can use like this
$http = new CHttpRequest();
$referrer_url = $http->getUrlReferrer();
$this->redirect($referrer_url);
Hope this will help you
You can also do like that
Yii::app()->user->returnUrl = Yii::app()->request->urlReferrer;
$this->redirect(Yii::app()->user->returnUrl);
For those who still struggling with returning to previous page: You have to create a property in login model $referer, and at the time when you initialize your Login model you set this property to Yii::$app->request->referrer. You have to go this way because after you submit your form your referrer does change to current page, and since it is null, it return you to index action. So the way how to pass the return URL is to store it in the hidden field at the form, so when you submit the form you have this URL loaded and you can perform action to return $this->goBack($form->referer ? $form->referer : null).
Here is the code:
Login model:
public $referer;
Controller action:
$model = new Login();
$model->referer = Yii::$app->request->referrer;
...
if($model->load(Yii::$app->request->post()){
...
if($model->save()){
return $this->goBack((($model->referer) ? $model->referer : null));
}
}
View login.php:
<?= $form->field($model, 'referer')->hiddenInput()->label(false) ?>

what is function redirect() in php

I use a code to connect gmail and get my friends list. In that code there is a function call
redirect('https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token='. $oauth->rfc3986_decode($accrss_token['oauth_token']), 'location');
I've searched for function redirect() but didn't find it in the php manual. Is it a built in function in php?
The 2nd parameter is 'location' what is the use of that parameter?
Here is the function where it's used:
public function connect_google($oauth=null){
if(!$oauth)
{
return null;
}
//create a gmailcontacts objects
$getcontact = new GmailGetContacts();
$accrss_token = $getcontact->get_request_token($oauth, false, true, true);
$this->ci->session->set_userdata('oauth_token', $accrss_token['oauth_token']);
$this->ci->session->set_userdata('oauth_token_secret', $accrss_token['oauth_token_secret']);
//redirect to google auth
redirect('https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token='. $oauth->rfc3986_decode($accrss_token['oauth_token']), 'location');
}
It is part of the CodeIgniter URL helper. See:
http://codeigniter.com/user_guide/helpers/url_helper.html
From the documentation:
Does a "header redirect" to the URI specified. If you specify the full site URL that link will be build, but for local links simply providing the URI segments to the controller you want to direct to will create the link. The function will build the URL based on your config file values.
As you've said, it isnt a built in function, so we dont know what it should look like.
However, considering the name i guess it should look like this:
function redirect($url, $header)
{
header("$header: $url");
}
Since sending a Location: {ur} header will redirect your page to another.
Use header: http://php.net/manual/en/function.header.php
header('Location: urlOfDestination');
It is probably a user defined function. It probably works hand in hand with header() hence the name, the first parameter is the page to redirect to, and the second one is to tell the function that it is indeed a redirect to the location. Just check out the header function.

Categories