Javascript window.open() but in Laravel - php

Im developing a website where, in a given moment, i need to redirect the user to an url passing a '_system' parameter. I know this in javaScript is something like window.open('xxx.com', '_system'), but i wanna know if there is way to do this using Laravel and its redirect() response helper and how to do that.

You can just pass the external url into the redirect() method and it will redirect to that url.
For example: return redirect('https://www.google.com');

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 to check the url in the controller slim-framework

I have problem with slim
I have the controllar, and 2 routs send to this controller.
The page html with teig.
The api and show that in json.
Now I need to check, if I request with this url:
/product/{slug}
return the twig (I do the return).
and if I request to url:api/product/{slug}
return me the json (i do the return).
now I don't know how to the the if request.
and how I just to know check what url request that controller.
$request->getUri()->getPath() will give you the current URL.
$request->getAttribute('route') will give you the current Route object.

Change URL in CakePHP

I have a Page in Cakephp that looks like
www.example.com/posts/next_posts
i.e. I have app/View/posts/next_posts.ctp
but I have Categories1, Categories2, Categories3,.... in Database.
How do I change the URL so that I assign a Variable of Database and it looks like
www.example.com/categories1/next_posts
www.example.com/categories2/next_posts
www.example.com/categories3/next_posts
By using the router. Read this chapter:
http://book.cakephp.org/3.0/en/development/routing.html
It will map a URL to a controller and action and passes arguments based on the URL. There are examples on that page as well if you're looking for that.

Laravel 4 PHP redirect header

How can I do PHP redirect header in Laravel 4?
In original PHP:
header('Location: http://www.example.com/');
I don't want to do with return Redirect::route('loginpage');. Because it does not work well with ajax. So, how can I do the PHP redirect with Laravel?
Thanks.
If you're wanting to get the url and refresh the page with an ajax function you could simply return the laravel route url and refresh the page with javascript:
// Use the route helper function
return route('loginpage');
// Javascript page redirect
var url = 'Your ajax response here';
window.location.href = url;
return Redirect::to('loginpage')
Is the most common way. Why doesn't that work well with ajax?
When using AJAX, your request isn't handled the way you're expecting it to be. This isn't a laravel-specific issue, as your header() location example would not work either.
If you want to make an ajax request that redirects the user, you'll need to have the response from ajax return something that you can key off of in javascript that executes a window.location change based on the results.

Difference between $this->render and $this->redirect Symfony2

What is the difference between $this->render and $this->redirect. Is there a way i can pass arguments with $this->render like in I do with $this->redirect
return $this->render('MedicineUserBundle:User:home.html.twig', array(
'info' => $all,));
Can I do something like this :-
return $this->redirect($this->generateUrl('MedicineUserBundle_login', array(
'info' => $all,)));
Or is there any other way I can pass values with $this->redirect to my template twig file.
And One more question How can i change the url with $this->redirect, eg If i dont have to pass any values to my template file I can do as mentioned above the render will take me to a page like localhost/myproject/home but $->this->redirect will execute the controller but the url wil be the same like localhost/myproject/. Is there anyway i can redirect to another url using redirect
Redirect()
Redirect performs a 301 or 302 redirect to the specified route/location. You can use this to pass in a full URL I believe. Using this method will cause the URL to change in the address bar.
Because Redirect uses a simple 301/302 header to do the redirect there is no way to pass template parameters to the new location except for on the URL as you would do to any controller or URL.
Render()
Render just renders up the template file you tell it to as a response to the current request. With this you can pass in your array of template parameters as normal.
Forward()
There is also Forward which will forward the request to another controller internally sending that controllers response back as the response to the current request without any redirects. Using this method re-routes the request internally without changing the URL in the address bar.
The key difference here between Render and Redirect is that Render is part of the View system and therefore can pass parameters to the tempaltes. Redirect is part of the Controller system and doesn't know anything about the View. You can pass parameters to the route or URL you are redirecting to but the target location must then do something with them to pass them on to the View.

Categories