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

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
}

Related

How to achieve this kind of url routing in codeigniter

i want to achieve url routing something like www.example.com/alicia
suppose alicia is not a class name or method name just something like passing data in url and with some class i want to access this and want to use it for further process.How i can use it ? Thanks in advance.
You can use Codeigniter's built-in routing, the file route.php is located in your config folder.
there you can add:
$route['alicia'] = 'welcome/index/something';
$route['alicia/:any'] = 'welcome/index/someotherthing/$1';
then in your controller, for example welcome you just create a function like:
public function index($page = null){
if($page=='something'){
// do what you need to do, for example load a view:
$this->load->view('allaboutalicia');
}
elseif ($page=='someotherthing'){
// here you can read in data from url (www.example.com/alicia/2017
$year=$this->uri->segment(2); // you need to load the helper url previously
}else{
// do some other stuff
}
}
documentation on routing and on urlhelper
edit after comment:
in case your uri segment is representing a variable, like a username, then you should use a uri scheme like www.example.com/user/alice and create your route like:
$route['user/:any'] = 'welcome/index/user';
then in your controller welcome
public function index($page=null){
if($page=='user'){
// do what you need to do with that user
$user=$this->uri->segment(2); // you need to load the helper url
}
else{
// exception
}
}
This could be tricky because you don't want to break any existing urls that already work.
If you are using Apache, you can set up a mod_rewrite rule that takes care to exclude each of your controllers that is NOT some name.
Alternatively, you could create a remap method in your base controller.
class Welcome extends CI_Controller
{
public function _remap($method)
{
echo "request for $method being handled by " . __METHOD__;
}
}
You could write logic in that method to examine the requested $method or perhaps look at $_SERVER["REQUEST_URI"] to decide what you want to do. This can be a bit tricky to sort out but is probably a good way to get started.
Another possibility, if you can think of some way to distinguish these urls from your other urls, would be to use the routing functionality of codeigniter and define a pattern matching rule in the routes.php file that points these names to some controller which handles them.
I believe that the default_controller will be a factor in this. Any controller/method situations that actually correspond to a controller::method class should be handled by that controller::method. Any that do not match will, I believe, be assigned to your default_controller:
$route['default_controller'] = 'welcome';

Laravel routing and route parameters

I have route defined in my route.php file like below:
Route::get('{test1}/{test2}/{test3}', function($test1, $test2, $test3) {
$result = [$test1, $test2, $test3];
return view('view', compact('result'));
});
it works fine in my controller but on the view part when i see it in the browser when i just write something like this in browser:
http://localhost/mysitefolder/public/test1/test2/test3
it loads the view and pass all the data but it gets all of my assets like my stylesheets, images and scripts from a url like below:
http://localhost/mysitefolder/public/test1/test2/js/jquery.js
why is that happing?
thanks in advance!
Two solutions either use a / in the start of your URLs so that relative addressing is not used or use laravel's helper functions {{ asset('/js/script.js') }}.
When you do not use '/' in the beginning your url, it is treated as if it was a relative address and current location is added in its start.
Sometimes even '/' won't work if your application is not served on Root level. For example you have your application at http://localhost/yourapplication then / would refer to your localhost instead of application, that's why best way is to use laravel's helper function.

Generating relative urls with laravel

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);

Define base url for laravel

I am new to laravel. My url for my only controller is
http://localhost/myapp/public/users
In the views for the form functions, I don't want to have to do an absolute path to the post function, e.g.
<form method='post' action='/myapp/public/user/update-user'>
I want to just set the users controller as the base url path so I can say in my form
<form method='post' action='/update-user'>
Is this possible?
Use URL::to()
or simply use laravel form
{{Form::open(array('url'=>'route_name'))}}
You can use URL::to() function.Laravel supports RESTful API or RESTful routing. So it is not a good idea to use routing like this.So use like,
In app/routes.php
// for view (GET)
Route::get('users', function(){
View::make('users');
});
Route::post('users', function(){
// do form validation and process here or call a function from controller
});
Then in your form use,
echo Form::open();
// form elements
echo Form::close();
Yes, it is possible, but it's a little complicated because laravel isn't really set up to support this.
You need to change the file vendor/laravel/framework/src/Illuminate/Http/Request.php, replacing the function root() with this:
public function root($domain = NULL)
{
return rtrim(($domain?:$this->getSchemeAndHttpHost()).$this->getBaseUrl(), '/');
}
And then in the getRootUrl($scheme, $root = null) method in the file vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php you need to change this line:
$root = $root ?: $this->request->root();
to this:
$root = $this->request->root($root);
That should allow laravel to automatically detect and handle subfolders correctly, so you will be able to use the standard URL methods.
First, you need to hide the /public/ part of your URL using the virtual host or the alias in your web server (apache or ...). The entry point (http://some.host/alias/ or http://some.virtual.host/) should lead directly to the public folder (i.e. /whatever/path/myapp/public/).
Second, using the routes, you can do the remaining easily. The route
Route::get('update_users', "UsersController#updateAction")
will redirect the http://whatever.host/update_users to the correct action without the need of the ugly format.

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

Categories