How to add pass route param to url view helper - php

I've created routing based on Hostname but reached trouble with URL generation when using \Zend\View\Helper\Url.
When I run:
$this->url('main/faq')
helper creates URL without lang param in subdomain. Ok, I can use:
$this->url('main/faq', [], [], true)
and then lang param is attached to subdomain, but this call makes a lot of mess (as it keeps other parameters that needed no more).
Is there any solution to attach lang param to subdomain without setting $reuseMatchedParams? Or to use it without mess?

Related

Slim 4 - Base path as query param

I have a Slim 4 app running and set some recurring url param as a base path to not show this in url generation.
Request flows:
Call to http:demo.xyz?p1=a ---> rewrite to http://slim.app/url/demo.xyz?p1=a through a Varnish proxy
Call to http://slim.app/url/demo.xyz?p1=a directly
Both calls need to work.
Inside a middleware I set /url/demo.xyz as base path so the URLs generated are http://demo.xyz?p1=a.
The problems with this are:
The base path is not recognized by routes.php as something to pass params, so url/abc is not matched.
I still need to have the value of the url param to fetch some things inside the Slim app.
My routes need to match this structure to work with both request flows.
$app->group('/url/{url}', function (RouteCollectorProxy $group) {
$group->get('[/[/city/{city}]]', IndexAction::class)->setName('home');
//...
});
Any idea to get this to work?
This is connected to the question placed here.
In the middleware, you can use Request::getUri() method to get entire uri. Then, if matches with first url format "http:demo.xyz?p1=a" do:
return $response
->withHeader('Location', 'https://www.example.com')
->withStatus(302);
if matches to "http://slim.app/url/demo.xyz?p1=a" do needed stuff to proceed.

Laravel call controller based upon query string or post variables

I am creating APIs for an app. Now app developer wants me to create a fixed base url and pass the ROUTE NAME (Which will point to controller function) as POST variable. Example:
http://example.com/Api
and POST variables like:
action=>'ROUTE_NAME'
But in laravel we can define the routes based upon the url parts as:
http://example.com/Api/ROUTE_NAME
I have tried using a single controller and loading the other controllers based upon SWITCH statements. But that doesn't seem to be a standard practice as i need to add switch condition every time I'll create a new API. Also middleware will not work on the loaded controllers dynamically.
Is there a way in laravel to achieve this? I am using laravel 5.4
You could implement a middleware that listens on the /Api route, which gets the ROUTE_NAME from the $request, then you could use the Route() helper function to find the url of that named route, then redirect the request to that route.
Something like:
// Generating ROUTE_NAME url...
$url = route($request->route_name);
// Redirect to that route...
return redirect()->route($url);
Obviously you'll need to add code to handle if it doesn't find a route etc, maybe return a json response back with a proper error code etc.

Create a Dynamic URL and Redirect to it in Laravel

So basically this is what I need. I have a router definition like this.
Route::get('view/{postId}', 'PostController#view');
Above router definition will get triggered if the url we request is www.domain.com/view/4. But what I want is I want to appear this url like www.domain.com/[category-of-post]/[titile-of-post] (Ex : www.domain.com/music/easy-chords-in-guitar).
In PostController, I will get the post using the id passed and thus can generate the url what I need. Here is the problem begins. As you can see, I need to redirect to a dynamic url which will look differently for each post. I want to redirect to this dynamic urls but these urls are not defined inside routes.php.
Is this possible in Laravel ?
In short, what I need is, I want to update Illuminate\Routing\RouteCollection::$route array with my dynamically generated url value with corresponding controller action in run time before I am invoking Redirect::to('someurl')
If you need further clarification, I will do it for sure. Please give me your suggestions.
it is simpler than you are thinking.
Route::get('{category}/{title}',['uses' => 'FooController#bar']);
This should be the last route defined in your route list. Any other route should go upper than this one.
this will match www.domain.com/music/easy-chords-in-guitar
rest routes define as you want.
e.g.
Route::get('/',['uses' => 'FooController#home']);
Route::get('about',['uses' => 'FooController#about']);
Route::get('contact',['uses' => 'FooController#contact']);
Route::get('{category}/{title}',['uses' => 'FooController#bar']);
route :
Route::get('action/{slug}', 'HomeController#actionredeemvoucher')->name('home.actionredeemvoucher');
Function in controller:
public function actionredeemvoucher($slug)
{
print_r($slug);
}

Yii Controller As Variable In URL

Using the PHP framework Yii I want to have a url like url.com/xyz where xyz is an argument I want to pass to a specific controller (I wont know the value of this argument as it is dynamically generated). Is this possible? To my understanding Yii will see xyz as a controller and I am not sure how to change this without destroying the functionality of the rest of the site (url.com/controller/arg/etc)
Help? :)
Obviously you can do this. For example the if you set a url rule
'<view:\w+>'=>'site/page',
will treat anything of url.com/xyz to the view parameter of the action page of the controller site.
THE PROBLEM IS THAT the for every other routes you have to specify an action , or in other words defaultAction of any other controller has no effect.
ie the url url.com/site WITHOUT the above rule will redirect to the site/index and to site/page with view="index" WITH the above rule
This is explained in the "URL management" chapter of the guide: http://www.yiiframework.com/doc/guide/1.1/en/topics.url
A rule like the following would route a request to example.com/foo to the action myaction in mycontroller, giving the value foo to the parameter name.
'<name>' => 'mycontroller/myaction'
This rule should be the last one in the rule list. Otherwise all requests would be passed to myaction, breaking existing parts of the site.

Why ZendFramework $this->getFrontController()->getBaseUrl() is returning empty string inside a controller?

I'm using ZendFramework 1.11.11 for my application and I need know the URL (more specifically the path) that fire up my controller inside my controller.
When I try
$this->getFrontController()->getBaseUrl();
an empty string is returned. I test that code with the default application created by ZendStudio and accessing it typing http://test.com/default/index/index and nothing.
Any idea how get the URL that fireup a controller inside the controller?
The base url is the offset from the original slash that you've set up in your configuration. You do not have a base url, as default is the name of the module, index is the controller, and index is the action.
For firing up another controller, check out this link http://framework.zend.com/manual/en/zend.controller.action.html#zend.controller.action.utilmethods
To get the current path in a Zend fashion from a controller, do the following.
$this->getRequest()->getPathInfo();

Categories