Laravel/Vue - Pass URL to Component - php

I'd like to pass a route('name', $id) to a vue component. In this case, this would be quite easy, since its only one URL with the parameter.
But what do I have to do if I'd like to pass in an array of URLs? How to tell PHP/Laravel to make an array, where the URL is present without the $id part.
For example:
<custom-name
data="{!! $urlData!!}"
id="{!! $id !!}">
</custom-name>
And $urlData consists of ['url1'=> 'test/{id}/abc', 'url2'=> 'test/{id}/xyz'].
The Plan is that I can build a vue component where I loop through the URLs and build a table with links to those URLs in it. I need the possibility to mutate these URLs before they are put into the table. But I need it to be done in the component.
So the question is: if it is possible in laravel to get the (i would call it) raw URL data like it is present in Route::getRoutes() but only for routes, I need no all. To be more precise, the problem is that the route() function need the corresponding $id Value in order to return an URL. But i need the URI like test/{id}/xyz. So i can generate the URLs in the component. I try to do it the Laravel-Way-Of-Life and I really like not to hard code it.
Hopefully, someone could lend me some help. :)

As far as i understand you, you have a bunch of routes and you need their URLs, right? If that is the case you can access it with some helpers methods built in laravel, and i can see that you want it with query strings etc.
You can use access them via the request with the fullUrl() helper, or just the path() helper if you dont need the query string.
Further reading on this is here:
https://laravel.com/docs/5.6/requests

Related

Is it possible to create a single url from multiple values in multiple tables?

is it possible in php to create a single url from multiple values across multiple tables?
For example, I want to generate a url that looks like the following:
http://www.domain.foo/joseph&soap&engineer&english, with the first name and surname is read from the NAMES table and occupation is read from the JOBS table and language is read from the SPEAKS table..?
Not sure if this is yet the correct way to solve my task, but thought I'd ask the question before I spend more time on it.
That isn't the way url parameters work. They should be name value pairs. You are also missing the query string. So what a normal url would look like would be this:
http://www.domain.foo/index.php?firstname=joseph&surname=soap&job=engineer&language=english
This is how you should be thinking about and approaching the use of the GET method and the passing of url parameters. Often this gets confused with URL rewriting, which converts url's that omit script names and parameters but you should start with the basic functionality and worry about layering on url rewriting once your code is working.
The other thing you might be missing is the "routing" that comes with pretty much every framework that implements the MVC pattern.
For example, consider route to be an additional parameter that gets passed as route=section_of_site. In your example, it seems that this function is going to look up information on a user or person, so perhaps it would be route=person
So now your full url would be:
http://www.domain.foo/index.php?route=person&firstname=joseph&surname=soap&job=engineer&language=english
Even without a framework or controller class, you now have a route variable which you could use to include a script, or to execute a function or class function. You have all the variables you need in $_GET, and all that is left is to do whatever data persistence work you require and return your result.

Defining Laravel routes when certain query strings are present

Say I want to be able to submit a search form on any page that will append a ?s= to the current url but return a SERP: https://example.com/my-page?s=foobar. (I've seen a few sites do this instead of pointing to /search?s=.* - not the way I'd do it, but it illustrates my question.)
In my Laravel web.php routes, is there currently a way to register this other than maybe using a wonky regex?
<?php
Route::get('.+?\?.+?\bs={search}', 'SearchController#search');
// This regex is probably wrong, but you get what I was going for,
// and that kinda highlights why this is not an ideal way to do it
?>
This gets uglier when, say, you want to capture multiple $_GET params:
https://example.com/my-page?s=foobar&dept=clothing
I haven't found anything in Laravel docs that let you define query string options on the Route $uri parameter. The recommended option is to just use pretty URLs (https://example.com/search/foobar), but there are definitely valid use cases for keeping query strings.
1: Create a middleware (https://laravel.com/docs/5.7/middleware)
2: Validate incoming request using $request->query('s')
3: If validation successfull return to next else redirect to wherever or display an error response
That is very simple that depends on the parts of a url. The Route class uses the "path string" of the url and you try to use the "parameters" of the url.
https://doepud.co.uk/blog/anatomy-of-a-url
In modern websites you should work with that structure because you get a better URL structure and it's much better for SEO and search engines.
You should use the parameters only in the function you call for small things which you can call over the $request->get('s'); method but then you have to redirect or you have to work in that function.
So don't fight the framework and work in that structure what is defined from the framework that all people who know the framework know how to work with it.

Laravel optional routing parameter

I am trying to build up my routes file and for the application we are building we may have a route such as:
/services/{game}/{id}
Is there a way to have that {game} parameter, but not actually pass it to the controller? Its basically there, but doesn't have to be anything specific and the controller doesn't need to know its there, it's purely for the users eye to make their URL make sense.
That is perfectly possible, you can catch it in the controller without doing anything with it, however you will have to catch it.
Do what Bielco said or just minify your route using slugs instead 2 parameters.
For example:
Game: Skyrim Legendary Edition
Slug (unique): skyrim-legendary-edition
Your route: /services/game/skyrim-legendary-edition
In Laravel routes.php
Route::get('services/games/{slug}', 'ServicesController#showGame');

Laravel dynamic rerouting

Hey guys i have a little problem i need to solve and i cant seem to find a way to do so.
I have an app that need to use different databases dynamically according to which user uses it. I thought that i would give each user an URL that contains hes unique alphanumeric id. So the URL would be something like ww.mydomain.com/app/1kh1h3as/
So i have 2 problems:
where should i put the database switch code. Is it better of in config file or should i use it in model classes so i have use of URI class to parse out the id?
how can i make the router understand that it needs to offset all the calls by one segment so it wont go looking for 1kh1h3as controller and ww.mydomain.com/app/1kh1h3as/users/all will launch all() method within Users controller?
Use this
DB::connection('mysql')->config['database']='your_user_database_name';
Try
Route::any('app/1kh1h3as/(:bundle)', function() {
return 'Welcome to the Admin bundle!';
});

Should I use array or string in link param in CakePHP HtmlHelper?

CakePHP HtmlHelper link() method accepts 2 types of variable as second parameter (the link URL param).
Now I wonder if using array for the param, like
array('controller'=>'users','action'=>'login')
is slower than using string, like '/users/login'. Because Helper won't have to parse the array, just display the link.
If it is so, then what is the purpose of link() method? For now, I am using HtmlHelper::url() method with a regular <a> to display all of my links, to keep my template clean!
Please correct me :)
One reason is reverse routing: For examples, if you route '/blah' to array('controller'=>'articles','action'=>'index'). When you create the link with array('controller'=>'articles','action'=>'index'), cake can automatically output '/blah'. It may not sound very interesting; but if later on you change the route to '/foo', then the link() method can automatically change the output to '/foo'.
Another reason is: using array you can build the url in a programmatic fashion. It's not just controller and action, you also have prefix, named parameters, your own custom parameters if you create in routes, etc.
For now, I am using HtmlHelper::url() method with a regular <a> to display all of my links, to keep my template clean! Well, you are making it harder on yourself then :)
This provides a consistent and flexible method for creating hyperlinks, referencing controllers / actions and specifying dynamic options via associative arrays. Performance shouldn't be an issue unless you are iteratively creating links. Even then, array management and implosion is usually much more efficient than string concatenation.

Categories