Is there a way without changing the whole configuration of CodeIgniter to get the URI segment for function ($this->uri->segment(2)) as a regular query string instead of being directly mapped to a function?
For example, I would be forced to have a URL like this:
http://localhost/books/functionName/bookNumber
I would like to have the bookNumber number right after the controller name (books):
http://localhost/books/bookNumber If I have the URL like this it would map the bookNumber to a function name.
You could use CodeIgniter's URI Routing to achieve your desired URL format.
If you add the following route to application/config/routes.php, then any URL that is entered that matches the route on the left, will map to the controller/function on the right:
$route['books/(:num)'] = "books/functionName/$1";
This will map a URL such as http://localhost/books/123, to the functionName function, in the books controller, passing 123 as the parameter.
This assumes that your 'book numbers' are always numbers (it would not work for strings), as (:num) will match segments only containing numbers.
Related
I can't figure out how to rewrite an URL (using mod_rewrite) from the following form
https://example.com/foo/bar/123/asd/qwerty
to the following form
https://example.com/index.php?controller=foo&action=bar¶ms[]=123¶ms[]=asd¶ms[]=qwerty
There will always be a controller and action supplied, but the number of parameters after that may vary. I'm currently passing the entire 123/asd/qwerty as a string to $_GET['params'] in PHP, but I would now like to turn this string into an already split array instead.
What RewriteRule do I use?
I am not sure that what you need is a rewrite rule.
What mod_rewrite is doing, when applied is leading from
A: https://example.com/foo/bar/123/asd/qwerty to
B: https://example.com/index.php?controller=foo&action=bar¶ms[]=123¶ms[]=asd¶ms[]=qwerty
BUT : In the above case B is the original URL and A is the re-written one...which is just more user friendly. Since you do not pass a variable in URL you can not "GET" it something from it. I think the only way to do it is to grab the url and split it in parts using php
Is that possible to make route that accept string parameter only for specific string? Example :
Route::get('{user_tipe}/event', 'admin\EventC#index');
That the route, I want to make the user_tipe param is only allow to two string like admin and author. Is that possible?
You can do that using regular expression constraits on your route:
Route::get('{user_tipe}/event', 'admin\EventC#index')->where('user_tipe', 'admin|author');
admin|author is a simple regular expression that will match either the string admin or author
UPDATE
Here you can find how to use the route param constraints when using Route::group
I'm trying to paginate the search results returned by the query. I have the following URL:
blog/search?query=post/5
Where I am trying to get the value of $start=5 from the URL using:
$start = $this->uri->segment(3);
It is not returning anything.
Where removing ?query=post, i.e, blog/search/5 works fine. But I want to keep the query parameter and read the value.
That's because of the ? character in the URI. CodeIgniter URI parser (and any other standard URI parser) does not recognize what you have in mind. After the ? character, it's all query string, not URI segments. See the output of PHP's parse_url():
parse_url('blog/search?query=post/5')
[
"path" => "blog/search",
"query" => "query=post/5",
]
See? The first segment is blog, the second is search and the rest is the querystring.
You need to change your URI design in an standard way. For example:
blog/search/5?query=post
So that the call to $this->uri->segment(3) will return what you have in mind.
Speaking of CodeIgniter pagination library, see reuse_query_string and page_query_string configs in the documentation.
I know Laravel5 Resource method will work like this.
TestControler#index /aa
TestControler#edit /aa/{aa}/edit
..
It's good to work if integer have been inserted.
/aa/1/edit -> work
But it will broken if string is coming.
/aa/aa/edit -> SQLSTATE[22P02]: Invalid text representation ..
So I wanna ask you the question is how should I allow request url thats integer only?
where should I write, route.php or Controller?
and how to abort 404 if string is coming.
any idea?
Expanding on my comment:
When working with Laravel's router, for any parameter you add to a URI definition (such as {id}), you can add a regex constraint. The constraint will take the variable value and test to see if the regex matches the value. If the regex fails, then the route will not be selected.
You do this using the where() method on the route and passing an associative array where the keys correspond to the variables in the URI, and the values are regexes to match. You can add constraints to as many variables in a route's URI as you like.
For example, if you wanted to constrain the id value in your URI to just numbers, you could do something like this:
Route::get("users/{id}", "Users#getUser")->where(["id" => "[0-9]+"]);
The documentation for this feature states:
You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression defining how the parameter should be constrained
See more examples in the documentation available here: https://laravel.com/docs/5.2/routing#parameters-regular-expression-constraints
Thanks to reply, Finally It works great.
But I wanna add this to my post.
Where method will work when I write 'standard' routing like this.
Route::get('/aa/{aa}/edit','TestsController#delete')->name('aa.edit')->where('aa','[0-9]+'); // works great!
But that's not work if I write 'RESTful' routing like this.
Route::resource('/aa', 'TestsController')->where('aa','[0-9]+'); // not work!
So I wrote this to app/route.php, It works very fine.
Route::pattern('aa', '\d+');
Route::get('/aa/{aa}/delete','TestsController#delete')->name('aa.delete')->where('aa','[0-9]+');
Route::resource('/aa', 'TestsController')->where('aa','[0-9]+');
Regular expressions have never been one of my strong points, and this one has me stumped. As part of a project, I want to develop an SEO link class in PHP. Handling the mod_rewrite through Apache is fairly straightforward for me, and that works great.
However, I'd like to create a function which is able to generate the SEO link based on a dynamic URL I pass in as the first (and only) parameter to the function.
For example, this would be the function call in PHP:
Blog Post Title
The function CreateLink would then analyse the string passed in, and output something like this:
blog/blog-post-title
The URL stub of the blog post is stored in the Database already. I think the best way to achieve this is to analyse the dynamic URL string passed in, and generate an associative array to be analysed. My question is, what would the Regular Expression be to take the URL and produce the following associative array in PHP?
link_pieces['page_type'] = 'blog/post';
link_pieces['post'] = 123;
link_pieces['category'] = 5;
Where page_type is the base directory and request page without extension, and the other array values are the request vars?
You can just use parse_url and parse_str, no need for regexes.
Use parse_url to break the URL into parts:
This function parses a URL and returns an associative array containing any of the various components of the URL that are present.
Then use parse_str to break down the querystring part of the URL.