Codeigniter dynamic variable routing - php

I am looking to dynamically re-route urls based on a parameter within the uri segment. For example if I have the following url:
domain.com/account/message/action/view
How could I go about creating a route that checked to see if the uri contained "action" and if so re-route to the value of the function + '_' + value of action?
domain.com/account/message_view
I've been reading through the routes documentation provided by ellislab and am still not quite sure how to go about doing something like this or if it would be better to use server rewrite rule? Any information greatly appreciated!

You would likely want to use the regular expressions availability in routes. Something along the lines of:
$route['([a-z]+)/action/([a-z]+)'] = "$1_$2";
Of course, that may not be exactly what you are looking for, but it should get you started.

Related

how to get the actual url in php while all pages are routing through index.php in MVC frameworks

When routing, all the php scripts request_uri showing only index.php.
I want to find the actual uri instead of index.php.
So,is there any php functions or variables that helps in getting the actual url.
I know they are different ways of getting the url for different frameworks but i want to know solution that works for every framework
Since you stated that you are using laravel, the solution below only works for this particular framework.
$current = url()->current(); // Returns the current url
If you need to pass parameters, getting the name of the route and building the url based on it works fine:
$current = URL::action(Route::currentRouteName(), ['arg' => 'value']); // Returns the current url and assign arguments to it

PHP URL Routing; Variables to 'directory'

Is it possible to change
host/index.php?variable=3
to
host/index.php/3
In my instance I'm using
host/bot?id=3
But I'd like it to be
host/bot/3 <-- This will be the only variable EVER used
How can I accomplish this, any links or code you can provide?
Thanks in advance!
GET /hotels/:id Hotels.Show
Segments of the path may be matched and extracted with a : prefix.
The :id variable above will match anything except a slash. For example, /hotels/123 and /hotels/abc would both be matched by the route above.
Extracted parameters are available in both the
Controller.Params map
and via Action method parameters.
https://revel.github.io/manual/routing.html

path as param in fat free framework

while routing in fat free framework, i need a URL as param
but i don't know how.
if i route to
GET /get-links/#url
#url can be any string WITHOUT /.
that means, if i open /get-links/www.muv.com it works. but if i open /get-links/www.muv.com/homepage.html the routing will not work.
i also tried GET /get-links/* - the routing works, meaning my function gets called.
but then how to get the #url (www.muv.com/homepage.html)?
and the next problem is: how to enter http://www.muv.com/homepage.html as #url
You may want to escape the characters that give you problems. For URLs there is a standard for this. PHP has rawurlencode. Maybe this helps.

Redirect & Route wildcard URL - with underscores to hyphens

I'm using CodeIgniter and I needed to redirect several urls with underscores to their equivalent with hyphens.
/some-controller --> /some_controller
I've partially solved this issue by tweaking the config/routes.php file.
The thing is :
How would I expand that to a Controller's function WITH a parameter.
Let's say I've got a controller some_controller (some-controller redirects to some_controller) and a function func in it (optionally taking a param).
$route['some-controller'] = 'some_controller';
$route['some-controller/func'] = 'some_controller/func';
This works. But, what if I have some-controller/func/someparam. (and someparam can be anything).
How could this redirect be implemented?
Basically what I need is a redirection from :
some-controller/func/*
to
some_controller/func/*
Any ideas?
Hint :
I don't need anything complicated like this one (
How to replace underscores in codeigniter url with dashes? ).
Hadn't searched enough - I admit it (it's one of those issues that always seem more complicated than they should be) :
http://codeigniter.com/user_guide/general/routing.html
$route['some-controller/func/(:any)'] = "some_controller/func/$1";
Or you could use (:num), if what you're expecting is a number...

website url's with step=1/2/3/4?

I see this alot appended to the url of a website, and when the step changes so does the page content. can anyone explain this to me in php.
I do not understand how it works.
The numbers of the step are $_GET variables. For example if your link is www.yourdomain.com/index.php?step=3 you can fetch this step by writing (in index.php):
<?php
$step = 0;
if (isset($_GET['step']))
$step = $_GET['step'];
?>
This declares a variable named $step and assigns the value from your URL to it (if any is set, else it's just 0).
You can then use this step variable value to show a specific page to your user. For example by fetching data on the flow from a database.
If you wan to transform this url:
www.yourdomain.com/index.php?step=3
into something nicer like
www.yourdomain.com/step/3
you will have to make use of a .htaccess file. They allow you to rewrite URL's, but as that can be pretty complicated I advise you to look around on the internet for more information about them. Such as here.
This is url rewriting.
You can do that via a .htaccess file with RewriteRule.
For exemple you can transform a url like http://example.com/a/b into something like http://example.com/script.php?arg1=a&arg2=b.
Often there is some magic URL rewriting happening behind the scenes.
This mostly just does something like this (taking your example):
request url: www.example.com/1/2/3/4
rewritten url: www.example.com/handler.php?a=1&b=2&c=3&d=4
So basically you are (often) not browsing any directories, the url YOU see is just to make it a little bit more attractive than what the SERVER sees.

Categories