Generating relative urls with laravel - php

Is it possible, with laravel 4, to generate relatives url to use in forms and links ?
For example I have a form where I'd like to use a relative url to /user/login and laravel build the form with the full url http: //url.com/user/login

I think what you need is helper functions.
Laravel helpers
secure_url
Generate a fully qualified URL to a given path using HTTPS.
echo secure_url('foo/bar', $parameters = array());
url
Generate a fully qualified URL to the given path.
echo url('foo/bar', $parameters = array(), $secure = null);

Related

How to add pass route param to url view helper

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?

Is there any built-in convenient mechanism to generate relative URLs?

1- Laravel url function generates a fully qualified URL to the given path. I need to generate relative path and only prepend them the site base url. I seems unlike other well-known framework it does not have a config to set the base URL. What is the correct way to generate relative paths?
2- On the other hand, if, for example, the user requests http://url/base/user/1/edit url, how to only get the user/1/edit part?
I don't remember
$uri = Request::path();
or $url = $request->path();
You can try. Hope this help
You may use URL::to('/'); anywhere in your app to refer to the base url and to get the part of the url you can use :
public function someFunction(Request $request)
{
$firstpart = $request->segment(1);
$secondpart = $request->segment(2);
// and so on
}

How to know which controller is called using URL in codeigniter

I am using routing like this
$route['Advertisement/1.0/(:any)']="v1/$1";
$route['Advertisement/1.1/(:any)']="v1_1/$1";
eventually both of them just do same work but i have to maintain both of them because of just response is same.
All is i want to know is how do i get to know the which controller is called using URL .If i get to know the URL like so will change the response accordingly so i don't need to maintain two controllers
1.0 or 1.1
I hope you understand what i am trying to ask.
Thanks in advance.
According to Codeigniter's User Guide, If you want to know the URL which is hit, then use:
$uri_segments = $this->uri->uri_string();
To get the URI segments.
Also, you can use current_url() URL helper to get the full URL (including segments); To do that:
// Load URL helper first (or use autoload config)
$this->load->helper('url');
// Get the current full URL
$url = current_url();
And if you want to get a specific segment of URI, use:
// "n" is the segment number you wish to retrieve,
// in this case, n = 2 gets '1.0' or '1.1'
$segment = $this->uri->segment(n);
Assuming your URL looks like this: example.com/Advertisement/1.0/...
$this->uri->segment(2);
will return 1.0 or 1.1
If i understood correctly, you can get the controller name and method name by using the following CI functions
$this->router->fetch_class(); // to get controller
$this->router->fetch_method(); // to get method

Generating url relative to the base url in laravel 4

I'm new to Laravel & right now building one application on L-4 but got stuck at one place. Can't able to understand how to generate url relative to base url. In laravel-3 i know this can be done by
$url = URL::to('user/profile');
But, in L-4 how we can do this.. ?
To generate a relative URL, you can use URL::route or URL::action as they allow to pass a $absolute parameter which defaults to true. So to get a relative URL when using named routes for example, you can use the following:
URL::route('foobar', array(), false)
This will generate a URL like /foobar.
First you need to create a Named Route like
Say yo want to go to http://baseurl/user and runs the method 'showuser' define in controller 'allusers'
then your Route shold look like this:-
Route::get('user', array('as' => 'myuser', 'uses' => 'allusers#showuser'));
Now your URL to /user would be
$myuserurl = URL::to('/myuser');
echo $myuserurl; // would be http://baseurl/user
I hope this helps you. Pls refer http://laravel.com/docs/routing#named-routes

How do I generate a friendly URL in Symfony PHP?

I always tend to forget these built-in Symfony functions for making links.
If your goal is to have user-friendly URLs throughout your application, use the following approach:
1) Create a routing rule for your module/action in the application's routing.yml file. The following example is a routing rule for an action that shows the most recent questions in an application, defaulting to page 1 (using a pager):
recent_questions:
url: questions/recent/:page
param: { module: questions, action: recent, page: 1 }
2) Once the routing rule is set, use the url_for() helper in your template to format outgoing URLs.
Recent Questions
In this example, the following URL will be constructed: http://myapp/questions/recent/1.html.
3) Incoming URLs (requests) will be analyzed by the routing system, and if a pattern match is found in the routing rule configuration, the named wildcards (ie. the :/page portion of the URL) will become request parameters.
You can also use the link_to() helper to output a URL without using the HTML <a> tag.
This advice is for symfony 1.0. It probably will work for later versions.
Within your sfAction class:
string genUrl($parameters = array(), $absolute = false)
eg.
$this->getController()->genUrl('yourmodule/youraction?key=value&key2=value', true);
In a template:
This will generate a normal link.
string link_to($name, $internal_uri, $options = array());
eg.
link_to('My link name', 'yourmodule/youraction?key=value&key2=value');
In addition, if you actually want a query string with that url, you use this:
link_to('My link name', 'yourmodule/youraction?key=value&key2=value',array('query_string'=>'page=2'));
Otherwise, it's going to try to route it as part of the url and likely break your action.
You can generate URL directly without define the rule first.
If you want to generate URL in the actions, you can use generateUrl() helper:
$this->generateUrl('default', array('module'=>'[ModuleName]','action'=>'[ActionName]'))
If you want to generate URL in the templates, you can use url_for() helper:
url_for('[ModuleName]/[ActionName]', $absolute)
set $absolute as true/false, dont forget to use echo if you want to display it.
But if you want to make a link (something like ), link_to() helper will do.

Categories