Laravel: how to set string as optional in route? - laravel

in web.php I have:
Route::get('car/id/{id}/color/{color?}', 'carController#getCar);
But I want the whole part of /color/{color?} to be optional not just the color parameter /{color?}, would you please tell me how to do this?

try this one
Route::get('search/{query?}', 'YourController#method')->where('query','.+')

Better To Used Query String Parameter here(now whole part should be optional by default)...
Route::get('car', 'carController#getCar);
example :
car?id=1&color=red
car?id=2
car
in Controller getCar Method
getCart() {
$id = request()->get('id');
$color = request()->get('color');
}

Related

Put default value in url

Hi I am trying to put default id value in my route url in Laravel. How do I achieve it? Please Help me. Thank you.
Route::get('manageattendance/{id}',[App\Http\Controllers\ManageAttendanceController::class, 'index'])-
>name('manageattendance');
replace {id} with {id?} and if you have id as parameter on the index function, set it to the default value like:
public function index($id = 1){
// your code
}

Not able to get Route parameter - Laravel 6

I have tried multiple solution but nothing worked yet, i am trying to get route Parameter in controller that was passed from a view.
Here is how i have created the route:
Route::get('addOptions/{questionId}', 'QuestionController#addOptions')->name('addOptions');
Here is how i am passing parameter to route from view:
Add Options
And here is what i am trying to get in controller but it's returning empty array:
public function addOptions(Request $request)
{
$allParameters = $request->input(); //not working
//$allParameters = $request->all(); //not working
//$allParameters = Input::all(); //not working
return $allParameters;
}
It returns empty array [] like this.
EDIT: But url at route addOptions look like this http://127.0.0.1:8000/admin/addOptions/4 in which 4 is questionId which means parameter is being passed but not retrieved.
What am I doing wrong here? Please guide, Thanks.
You should be passing the route like this:
Add Options
as for Laravel docs. the route params are passed an array with the key referencing the param
$url = route('profile', ['id' => 1]);
To retrieve the data in your controller, you should use:
$request->route()->paremeters()
or
$request->route('parameter_name')
If you want to pass the parameter
Add Options
I think your function parameters are wrong
You are passing question id from Route So your function should be like
public function addOptions($questionId)
{
$allParameters = $questionId; // you question ID passed throught Route
return $allParameters;
}

Creating models with many variables

Can anyone help me interpret this code?
What does $gallery = false mean? Why mention it instead of just erasing it?
models/post_model.php
public function post($post_parent_ID, $gallery = false)
{
}
If you want to pass any default value for any argument, you need to write it like this.
Then why it's false - Because by default, you don't like to use it. But you have kept an option for future use. Now when you pass second argument with any other value, it will work. Otherwise, your function will work by using first argument.
Function parameters can sometimes be optional, which means they don't need to contain any data.
public function post($post_parent_ID, $gallery = false)
{
}
Means that when I'd send something to this function I can do:
$this->post(1);
or
$this->post(1, 2);
Inside the post function you would check wether $gallery is filled or not depending on the use needed.

Difficulty with two segment uri

They are trying to create the url, where the first segment is the User and the second is his file, ex: www.exemplo.com/joao/ball
Controller
public function user() {
$user_url = $this->uri->segment(1);
}
^^ This would return the profile with every file: www.exemplo.com/joao
public function arquivo() {
$arquivo_url = $this->uri->segment(2);
}
^^ This specific file: www.exemplo.com/joao/bola
Routes
$route['(:any)'] = 'home/user/$1';
$route['??'] = 'home/arquivo/$1';
To solve your problem, you should use route like this..
$route['(:any)/(:any)'] = 'home/arquivo';
$route['(:any)'] = 'home/user';
but as far you with your project this type of routing will give your some hard time. i suggest you to use explicit route name because (:any) refer any thing can be pass through this url.
You can use routes to map the URI and its parameters to the relevant function. CodeIgniter routes behave differently depending on the version of CI.
In CodeIgniter 2.2.0 (:any) is equivalent to the regex, .+ - matches one or more of any character (excluding line breaks); in 3.0 and the current development version it is equivalent to [^/]+ - one or more of any character, excluding line breaks.
The latter is more useful in this case, as you want to identify the two parameters (separated by a forward slash).
In 2.2.0:
$route['([^/]+)/([^/]+)'] = 'home/arquivo/$1/$2';
$route['(:any)'] = 'home/user/$1';
In 3.0:
$route['(:any)/(:any)'] = 'home/arquivo/$1/$2';
$route['(:any)'] = 'home/user/$1';
Controller functions will usually pass the URI parameters as function parameters like this:
public function user($user)
{
// Show the user's profile
}
public function arquivo($user, $file)
{
// Show the file for the user
}
Able to resolve with the following code.
$route['([^/]+)'] = 'home/user/$1';
$route['(:any)/(:any)'] = 'home/arquivo/$1';

How to build an anchor in CodeIgniter where you want to change a variable that is already present in the URI?

Normally I would just use URL GET parameters but CodeIgniter doesn't seem to like them and none of the URL helper functions are designed for them, so I'm trying to do this the 'CodeIgniter way'.
I would like to build a page where the model can accept a number of different URI paramters, none necessarily present, and none having to be in any particular order, much like a regular URL query string with get parameters.
Let's say I have the following url:
http://example.com/site/data/name/joe/
Here not including the controller or the method there would be one parameter:
$params = $this->uri->uri_to_assoc(1);
print_r($params);
// output
array( [name] => [joe] )
If I wanted 'joe' to change to 'ray' I could do this:
echo anchor('name/ray');
Simple enough but what if there are more parameters and the position of the parameters are changing? Like:
http://example.com/site/data/town/losangeles/name/joe/
http://example.com/site/data/age/21/name/joe/town/seattle
Is there a way to just grab the URL and output it with just the 'name' parameter changed?
Edit: As per landons advice I took his script and set it up as a url helper function by creating the file:
application/helpers/MY_url_helper.php
Basically I rewrote the function current_url() to optionally accept an array of parameters that will be substituted into the current URI. If you don't pass the array the function acts as originally designed:
function current_url($vars = NULL)
{
$CI =& get_instance();
if ( ! is_array($vars))
{
return $CI->config->site_url($CI->uri->uri_string());
}
else
{
$start_index = 1;
$params = $CI->uri->uri_to_assoc($start_index);
foreach ($vars as $key => $value)
{
$params[$key] = $value;
}
$new_uri = $CI->uri->assoc_to_uri($params);
return $CI->config->site_url($new_uri);
}
}
It works OK. I think the bottom line is I do not like the 'CodeIgniter Way' and I will be looking at mixing segment based URL's with querystrings or another framework altogether.
You can use the assoc_to_uri() method to get it back to URI format:
<?php
// The segment offset to use for associative data (change me!)
$start_index = 1;
// Parse URI path into associative array
$params = $this->uri->uri_to_assoc($start_index);
// Change the value you want (change me!)
$params['name'] = 'ray';
// Convert back to path format
$new_uri = $this->uri->assoc_to_uri($params);
// Prepend the leading segments back to the URI
for ($i=1; $i<$start_index; $i++)
{
$new_uri = $this->uri->segment($i).'/'.$new_uri;
}
// Output anchor
echo anchor($new_uri);
I'd recommend wrapping this in a helper function of some sort. Happy coding!
Why not use CodeIgniter's built in URI Class? It allows you to select the relevant segments from the URL which you could use to create the anchor. However, unless you created custom routes, it would mean that your methods would need to accept more parameters.
To use the URI Class, you would have the following in your method:
echo anchor($this->uri->segment(3).'/ray');
Assuming /site/data/name are all CodeIgniter specific (/controller/method/parameter)
Now, I think this could be made a lot easier if you were using routes. Your route would look like this:
$route['site/data/name/(:any)'] = 'site/data/$1';
Effictively, your URL can be as detailed and specific as you want it to be, but in your code the function is a lot cleaner and the parameters are quite descriptive. You method would defined like this:
function data($name) { }
To extend your route to accept more parameters, your route for the the example URL "http://example.com/site/data/age/21/name/joe/town/seattle" you supplied would look like this:
$route['site/data/age/(:num)/name/(:any)/town/(:any)'] = 'controller/data/$1/$2/$3';
And your function would look like this:
function data($age, $name, $town) { }

Resources