Laravel: how to limit some route parameter to specific values? - 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

Related

How should I do Exception on Laravel5 Resource Controller?

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]+');

PHP: Url as parameter

I'm building a small restful api and I'm asking if it's possible to seperate the url to php file and the end of the url.
E.g. www.mydomain.com/api/parameter/1/2/
In this case the php file is adressed with www.mydomain.com/api/ or www.mydomain.com/api/index.php and parameter/1/2/ is the parameter.
I want a CRUD interface so that GET without parameter gets a list of all data. To achieve this I need to check if a parameter is attached and to extract the parameter.
Other example
www.mydomain.com/topics/ => gets all topics
www.mydomain.com/topics/1/posts/ => gets all posts of topic 1,
www.mydomain.com/topics/1/posts/2/ => gets post 2 of topic 1
My question is: Is it possible and how?
You would probably have to read the request URI from the end of the URL using $_SERVER['request_uri']. This would return /api/parameter/1/2. You could then substring it if the length is reliable, or use a regex with preg_match to get just the parameter section. e.g.
preg_match("parameter\/.*", $_SERVER['request_uri'], $matches)
would return either the string parameter/1/2 in the $matches variable, or false if no match was found
But yeah like others are saying, you're probably better using GET parameters if you can, and just do a check using isset() to see if there are any parameters.

How to do an LDAP search on a muli value Attribute

I am trying to do a LDAP Search on a multivalue attribute ACL using PHP. But when I try to set a filter on ACL $filter="(ACL=*$cn*)" where $cn = prnman03 there are no results returned.
ACL - 16#entry#cn=prnman03,ou=ipp,ou=services,o=uct#[Entry Rights]
3#entry#[Root]#iPrintPrinterIPPURI
8#entry#ou=backup,ou=ipp,ou=services,o=uct#iPrintPrinterUserRole
8#entry#ou=ippl,ou=ipp,ou=services,o=uct#iPrintPrinterUserRole
8#entry#ou=ipp,ou=services,o=uct#iPrintPrinterUserRole
cn - IPP00005
iPrintPrinterIPPURI- ipp://srvnbsidw001.uct.ac.za/ipp/IPP00005
If one of the attribute values matches the filter, then the entry will be considered to be returned (as long as permissions allow).
But remember that LDAP Filters are resolving in True, False, Undefined. Undefined means that there was no way to apply the filter and get a result. If there is no substring matching rule defined for the ACL attribute, then matching a substring filter will be undefined.
As you are doing this on an ACL for eDirectory, I do not think you will find and values that work for substrings. Even though their documentation say there are some conditions where matching will work, I have not be able to find or perform any such matches.

CodeIgniter URL Function Segment as Query String

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.

Parameter after url is not passed to called function when Parameter contains : or \

I am passing keyword inputed by user to
function search_result($input)
in cakephp fron Javascript
like this www.example.com/search_result/input from Javascript
where input is from user
It gives an error when input contains : as no arguments found for search_result. It working fine for other inputs.
You will probably want to encode the search term before passing it to PHP from Javascript (which I assume means you're using AJAX).
You can do this by using:encodeURIComponent:
encodeURIComponent(term);
In addition to URI encoding, the problem is that : is used to separate cake parameters, so when you have it in your search query, cake thinks it's a param.
But, there is a fix in the book, apparently, you can use the "trailing star syntax":
Router::connect(
'/search_result/**', // notice two stars instead of one
array('controller' => 'search', 'action' => 'search_result')
);
This should pass everything after the /search_result/ as a single param.
Hope that helps.

Categories