Codeigniter 4 Routes conflict - codeigniter

$routes->get('MATCH ANYTHING WHICH STARTS WITH sell-', 'Home::navigator/$1');
I want to match any URI which starts with "sell-" without the quotes and redirect to the Home controller navigator method.
For instance, /sell-my-car should be redirected. /sell should not be redirected.
How do I accomplish it?

You can use regex to match a desired uri, like:
$routes->get('(^sell-.*)', 'Home::navigator/$1');
This will match everything starting with "sell-", but not "sell".
More on regex:
https://www.regular-expressions.info/

Related

How to pass file path with '/' to route laravel?

How can we pass file path to route like this,
<a href={{route('route.name',['path'=>'uploads/xyx/'.$id.'/'.$attachment_name])}}>download</a>
However, I would like this to hit
Route::get('download/{path},'Controller')->name('route.name')
When I hit this route my url got transformed somehow like this -> download/uploads/44/filename which is causing not found exception!
I want to pass the path as a parameter, where slashes should have to be ignored! so that I can get full path in my controller!
As per the documentation you can do this:
"The Laravel routing component allows all characters except /. You must explicitly allow / to be part of your placeholder using a where condition regular expression"
Route::get('download/{path}', ...)->where('path', '.*');
Laravel 7.x Docs - Routing - Parameters - Encoding Forward Slashes
use get parameter remove {path}
Route::get('download,'Controller')->name('route.name')
in blade
<a href={{route('route.name',['path'=>'uploads/xyx/'.$id.'/'.$attachment_name])}}>download</a>
this will generate url like route.name?path=download/uploads/44/filename
still u will get data in controller like $request->path

How to remove '/' from end of Sinatra routes

I'm using Sinatra and the shotgun server.
When I type in http://localhost:9393/tickets, my page loads as expected. But, with an extra "/" on the end, Sinatra suggests that I add
get '/tickets/' do
How do I get the server to accept the extra "/" without creating the extra route?
The information in Sinatra's "How do I make the trailing slash optional?" section looks useful, but this means I would need to add this code to every single route.
Is there an easier or more standard way to do that?
My route is set up as
get '/tickets' do
It looks like the FAQ doesn't mention an option that was added in 2017 (https://github.com/sinatra/sinatra/pull/1273/commits/2445a4994468aabe627f106341af79bfff24451e)
Put this in the same scope where you are defining your routes:
set :strict_paths, false
With this, Sinatra will treat /tickets/ as if it were /tickets so you don't need to add /? to all your paths
This question is actually bigger than it appears at first glance. Following the advice in "How do I make the trailing slash optional?" does solve the problem, but:
it requires you to modify all existing routes, and
it creates a "duplicate content" problem, where identical content is served from multiple URLs.
Both of these issues are solvable but I believe a cleaner solution is to create a redirect for all non-root URLs that end with a /. This can easily be done by adding Sinatra's before filter into the existing application controller:
before '/*/' do
redirect request.path_info.chomp('/')
end
get '/tickets' do
…
end
After that, your existing /tickets route will work as it did before, but now all requests to /tickets/ will be redirected to /tickets before being processed as normal.
Thus, the application will respond on both /ticket and /tickets/ endpoints without you having to change any of the existing routes.
PS: Redirecting the root URL (eg: http://localhost:9393/ → http://localhost:9393) will create an infinite loop, so you definitely don't want to do that.

Shorten URLs within CodeIgniter

This question has been asked a few times but I can't seem to find a solution that helps me which is why I am trying here.
I have my site setup with the following for URLs I am using CodeIgniter I have a controller called user which loads a user view.
So my URLs are structured as follows:
http://example.com/user/#/username
I want to try and strip out the user controller from the URL to tidy up my URL so they would just read:
http://example.com/#/username
Is this possible I have been looking at route and have tried lots of different options but none have worked?
$route['/'] = "user";
Could anyone offer any solution?
Assuming the '#' in your URLs is a valid function and 'username' is a parameter for that function, then this route should work:
$route['#/(:any)'] = "user/#/$1";
Depending on what usernames are to be routed you may want to change the wildcard. For example, if you only wanted to route numbers as the parameter, you could change (:any) to (:num).
(:num) will match a segment containing only numbers.
(:any) will match a segment containing any character.
You can also use regular expressions to define routing rules, allowing you to further restrict what is routed.

Codeigniter URL routing solution for my website

I have a website that is developed with CodeIgniter. I have added the route for my url as follows:
$route['about_us'] = 'about-us';
Now I have a problem with that. I.e. when I am looking for the url www.mysite.com/about_us it works and at same time www.mysite.com/about-us is also working. I want only one url to work: the one with the underscore.
I have removed this to:
$route['about_us'] = 'about-us';
But the url www.mysite.com/about-us still works. It may cause duplicate content for my website in Google and so more page links also showing. Even I don't have that functions too. Like www.mysite.com/about_us/design. Likewise in about_us controller file index function only there, but design method calling in Google.
How do I resolve this problem?
You actually don't need a route here. The normal purpose of request routing the way you are using it is so that you can use hyphenated URLs when hyphens are not permitted in class and function names. I.E. you want the url to by www.example.com/test-controller, but you can't actually name a controller test-controller because the hyphen is illegal.
If you only want to have the underscored URL such as www.mysite.com/about_us then just remove the route completely and name the controller about_us. With no routing rules the hyphenated url should 404.

Capture all URLs in Sinatra except /admin/

I'm trying to create an action that matches all URLs except for the /admin/ ones. I also want to capture the URL that was entered, but so far I can only do the first bit.
get %r{^(?!/admin/.*$)}
That's what I've got so far. This will make sure I get all URLs except admin, but then how do I capture what the URL was?
aha!
Figured it out :)
get %r{^(?!/admin/.*$)(.*)} do |content|
You can find the current path in request.path_info.

Resources