How to pass file path with '/' to route laravel? - 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

Related

Laravel - How do I navigate to a url with a parameter to left of subdirectory

I have to test this Route, but I am not sure how to navigate to it.
Route::get('/{type}/Foo/{page}/{date}', 'FooController#index');
I understand that URLs usually have subdirectories defined before the parameters in the URL.
I have worked with URLs like this example
Route::get('/Foo/{type}', 'FooController#index');
which would have an endpoint that looks like
/Foo?type=bar
Does anybody know how to test a route such as the one above?
Well i think that you need to clear out a bit the difference between route and query parameters.
In your case you are trying to use route parameters which will actually look something like:
/{type}/Foo/{page}/{date} => /myType/Foo/15/12-11-2021
Laravel considers the words inside {} as variables that you can retrieve via request so you can do something like:
$request->type
and laravel will return you the string myType as output.
In your second case that you have tried in the past you are referring to query parameters which are also a part of the $request. Consider it as the "body" of a GET request but i don't mean in any way to convert your post routes to GET :)
The thing with your second url is that:
/Foo/{type} is not similar to /Foo?type=bar
instead it should be like: /Foo/bar
In general query parameters are when you want to send an optional field most of the times in your GET endpoint (for filtering, pagination etc etc) and the route parameters are for mandatory fields that lead to sub-directories for example /Foo/{FooId}/Bar/{BarId}
The thing to remember is that you must be careful about your routes because variables can conflict with other routes.
For example a route looking like this:
Route::get('/foo/{fooId}', [FooController::class, 'getFoo']);
Route::get('/foo/bar', [BarController::class, 'getBar']);
will conflict because laravel will consider bar as the variable of the fooId so your second route can never be accessed.
The solution to this is to order your routes properly like:
Route::get('/foo/bar', [BarController::class, 'getBar']);
Route::get('/foo/{fooId}', [FooController::class, 'getFoo']);
So when you give as a route parameter anything else than bar your will go to your second route and have it working as expected.

Product And Category Separation In Route (Laravel)

I'm setting up a new route system.
Route::get('/{cat1Url}', 'CategoryController#showCat1')->name('showCat1');
Route::get('/{productUrl}', 'ProductController#showProduct')->name('showProduct');
My sef link is after "/"
But,
{{ route('showProduct',[$p->pr_url]) }}
This method not working with route name. Working only upside route.
I don't want use
"/cat/myVariable"
or
"/product/myVariable"
Can't I use route name to work this way?
What is the solution to this?
In this way, if you make a get request to /something the laravel you start from top of web.php file looking to a route that follows the pattern. Your both routes will follow that pattern, but the laravel will always, pass the first one to controller.
You have two options:
Put only one route, and inside the controller you switch to the appropriate function. But this isn't a great ideia, because this is the function of the Web.php.
Use the routes like the documentation recommend:
Route::get('/cat/{catId}', 'CategoryController#showCat')->name('showCat');
Route::get('/prod/{productId}', 'ProductController#showProduct')->name('showProduct');
and in Controller you make the appropriate handler of your Category or Product.
You will have to have a way to tell Laravel which url to be mapped to what otherwise it will always use the last defined route. So in your case calling /myVariable and /myVariable it will use the latest definition which is showProduct. The only other way is if you use regular expression to differentiate the variables. For example:
Route::get('/{cat1Url}', 'CategoryController#showCat1')
->name('showCat1')->where('cat1Url', 'cat-*');
Route::get('/{productUrl}', 'ProductController#showProduct')
->name('showProduct')->where('productUrl', 'prod-*');
This way your slugs need to start with what you define, but you cannot use just id as a numeric value for both.

CodeIgniter - How to route a controller with other name?

I have installed an authentication module named bit_auth in my codeIgniter.
I have a controller for that module named "bit_auth" so when calling functions on my controller my urls will be like this:
http://(mydomain.com)/bit_auth/
http://(mydomain.com)/bit_auth/edit_user/1
http://(mydomain.com)/bit_auth/activate/7b60a33408a9611d78ade9b3fba6efd4fa9eb0a9
Now I want to route my bit_auth controller to be called from http://(mydomain.com)/auth/....
I have defined these routes in my "config/routes.php":
$route['auth/(:any)'] = "bit_auth/$1";
$route['auth'] = "bit_auth";
It's working fine when I use: http://(mydomain.com)/auth/
but it shows me 404 page not found error when opening links such as:
http://(mydomain.com)/auth/edit_user/1
http://(mydomain.com)/auth/activate/7b60a33408a9611d78ade9b3fba6efd4fa9eb0a9
What am I doing wrong?
Because you are using more params than are in the route you will have to do this:
$route['auth/(:any)/(:num)'] = "bit_auth/$1/$2";
Hope this helps!
After googling and investigating I saw somewhere they where using another syntax for routing in CodeIgniter which was using regular expression directly rather than using codeigniter described patterns like (:any) or (:num) in its help. I just replaced (:any) with (.+) in my config/routes.php and now it is working perfect.
$route['auth/(.+)'] = "bit_auth/$1";
$route['auth'] = "bit_auth";
Because in CodeIgniter (:any) and (:num) are not including / and they are aliases for regular expression patterns of ([^\/]+) and (\d+) so if you want to match rest of the link including any number of / you can use manual regular expression pattern (.+) which includes / in its pattern and will trigger for the all rest of URL.

Magento - passing params in URL to template file

I have a template file at ../page/video.phtml and it's served at http://mysite/video.
I want to add params in the url to play different videos on that page. I can add it as a query string param, http://mysite/video?select=filename but I would prefer to use http://mysite/video/filename.
However, when I try this I get a 404. What would I have to do to achieve this?
I'm using Magento 1.7
You must explicitly include all the action parts (route, controller and action) in the URL before adding parameters this way, because when you use http://mysite/video/filename, Magento looks for the index action of the filename controller for the module having a front route named video (which does not exist, hence the 404 error).
From the URL you gave, a working URL would rather look like this : http://mysite/video/index/index/select/filename

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.

Resources