I am trying to pass a name variable via this routing pattern.
<controller:product>/<name:[^\/]+> => 'product/view'
My aim is to get the $name in my actionView of the product controller, it works most of the times, but what I've discovered is it fails to pass the name if it contains a sharp (#) , for example sending a request
/product/test%20#something
Will fail, as far as I understood it is trying to find actionTest instead.
It might be a solution to set the default action of the controller to go to the view, but I want a better solution for this. I am sure there is a yii way to solve this.
Also another solution is to urlencode($productName), but it makes the URL look more ugly.
Related
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.
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.
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
I have been used laravel, and I find it's by far the best PHP Framework there is. But even so, I still think that to be able to understand it and PHP MVC's in general, I need to make my own first.
So, as of now, I'm in the process of making my own MVC, I got most things covered. But I wanted to add a feature that is identical to Laravel, which is the Post-Redirect-Get feature, (or so I think).
What I mean is, for those unaware, that if a person visits a link, say localhost/project/laravel/public/profile using the Route::get('localhost/project/laravel/public/profile', 'SomeController#action) He will only be able to view the profile page, from the action() function in SomeController. But when he uses the Route::post('localhost/project/laravel/public/profile', 'SomeController#action2), only when does is the POST request sent from the localhost/project/laravel/public/profile URL, will the action2() function activate.
So, My question is,
How can I make my own Route::get() and Route::post() to work like in laravel
If you want have get and post in the same route you should try this methods
POST and GET in the same pattern
Currently in my CI project I have a single controller that handles all things account. Such-as register, login, activation, etc.
My routes work as such...
domain.com/account/login/ or domain.com/account/register/
How can I remove account from the route while also being about to remove the controller from other pages.
I basically want the controller to always be removed. One of my reasons for this is SEO, search engine rank the importunateness of a page based on how deep it is in a website.
The only way I have seem to achieve this is to do some thing like route['activate'] = 'account/activate'; for every single page, which would be a huge hassle.
$route['^(?!other|controllers).*'] = “account/$0″;
Try this :
$route['(:any)'] = "account/$1";
The answer to your question is that you DO have to explicitly set the routes.
How is it going to know which controller a given function is in????
You have to tell it.
use mod_rewrite (if the controller is always the same name)
Ok, I can think of one way to do this, but it is probably gonna be more of a pain than just writing out routes for each function.
You need to extend the Router.php with application/core/MY_Router.php and overide the _validate_request() method. Which basically decides if this this is a valid route or not.
it does a check to see if the controller class exists then fails if it doesn't exist.
You need to replace this with some code which assumes no controller segment, then scans thru each of your controllers and checks if it contains the method called (it will be segment 1, since theres no controller).
Now the tricky part, at this point in the CI lifecycle your controller obviously isnt loaded, so you cant examine it using method_exists() yet.
You need to load your controllers one at a time, and then for each one run
method_exists($loaded_class, $method_name)
and if its true, then set then go ahead and call:
$this->set_class('the_name_of_the_scanned_class_which_had_the_method');
Then CI can keep going on as normal and it will load your methods without the user ever know what controller it loaded from.
.. probably not worth the hassle imho. A much easier solution would be to just have one controller and one route to that controller.