i want to use url like: http:localhost:8000/api/students/?key=value.
My API is set up as follows:
Route::get('students/{key},'Controller#method')
but my url is: http:localhost:8000/api/students/value
Could anyone help me please?
If you want to pass the key as a $_GET param you want to change your route to just be:
Route::get('students/,'Controller#method')
That way you can use http:localhost:8000/api/students/ and pass any parameters you want
Change Your Route :
Route::get('students,'Controller#method')
in Your Controller use
$request->input('key') or $request->query('key')
public function method(Request $request){
$value = $request->query('key');
$value2 =$request->input('key');
echo $value;
echo $value2;
}
Route::prefix('api')->group(function () {
Route::get('students/{key}','Controller#method');
// here come api-prefixed routes
});
https://laravel.com/docs/5.6/routing#route-group-prefixes
Related
I have a controller with Index function, Now i am trying to make a more fundtion for on scroll pagintion in codeignitor Controller.
This is my function
public function load_more($cat2,$cat1)
{
$group_no = $this->input->post('group_no');
$content_per_page = 12;
$start = ceil($group_no * $content_per_page);
$all_content = $this->product__model->get_all_content($start,$content_per_page,$cat2,$cat1);
if(isset($all_content) && is_array($all_content) && count($all_content)) :
foreach ($all_content as $key => $content) :
echo '<li>'.$content->id.'</li>';
echo '<p>'.$content->ITEM_CODE.'</p>';
endforeach;
endif;
}
Controller name is Product
When i check this fucntion at my url its hows page not found...
this is URL
http://www.dezaro.com/Product/load_more/160/2
How can i resolve it ???
Siddharth vyas The issue is with your URL you are hitting. Use product instead Product.
So Use http://www.dezaro.com/product/load_more/160/2 instead of
http://www.dezaro.com/Product/load_more/160/2
It will work for sure. Because it take the filename instead of Classname.
Try this
$route['method/(:any)/(:any)'] = 'controller/method/$1/$2';
your route should be
$route['method/(:cat1)/(:cat2)'] = 'product/load_more/$1/$2';
It seemd you are facing issue of index.php
Please try using url:
http://www.dezaro.com/index.php/Product/load_more/160/2
please help me, this is simple thing but I don't know why this still keep error for an hour,
on my view I've got :
<a href="admin/editProduct?idd=$id">
on my controller that directed from above :
public function editProduct(){
$data["id"] = $_GET['idd'];
$data["produk"] = $this->model_get->get_data_list(2);
//this below doesn't work either, i just want to past the parameter to my model
//$data["produk"] = $this->model_get->get_data_list($_GET['idd']);
$this->adminHeader();
$this->load->view("adminPages/editProduct", $data);
$this->adminFooter();
}
I can not use the array id. It keeps telling me undefined variable idd.
I don't know what to do anymore, please anyone help!
I am using Codeigniter framework
Change your view as:
<a href="admin/editProduct/<?php echo $id;?>">
And in the controller, either get the id as parameter,
public function editProduct($id) {
}
or as uri segment
public function editProduct() {
$id = $this->uri->segment(3);
}
Change your link (in view) with this
<a href="admin/editProduct/$id">
And change your controller as
public function editProduct($id) {
}
Then user $id inside your controller
Make the href link as follows:
...
And edit the controller like this:
public function editProduct($id){
...
$this->model_get->get_data_list($id);
}
Where $id will be the passed $id.
make
try this this works fine
public function editProduct()
{
$id=$this->uri->segment(3);
$data["produk"] = $this->model_get->get_data_list($id);
$this->adminHeader();
$this->load->view("adminPages/editProduct", $data);
$this->adminFooter();
}
This code $route['basketball'] = "controller/product/?id=7" does not work.
function product()
{
echo $_GET['id'] // no output
}
How to describe the rules in the route?
If possible use CodeIgniter's standard URL routes.
In your case:
$route['basketball'] = "controller/product/7";
function product()
{
}
OR if $_GET['id'] needs to be dynamic
$route['basketball/:num'] = "controller/product";
function product($id)
{
}
Hope that helps.
Because you are in PHP , you can basically set $_GET and $_REQUEST parameters which are super global variables that can be accessed anywhere in the code.
So you can do a callback and set them there.
For example:
$route['basketball'] = function(){
$_GET['id']=$_REQUEST['id'] = 7;
return "controller/product/";
};
Then in your code you can access $_GET['id'] or whatever.
newbie in laravel.
In laravel sample routing
Route::get('books/{genre}', 'controller#method');
The link would be something like this
link.com/books/crime or link.com/books/programming
how do i do it if I want to get both genre?
if this possible is to achieve
link.com/books?genre=crime,programming
how do i write that in routes? and also how do i get the value in controller?
I have tried something like this. But I don't have any idea how to achieve it.
Route::get('books?{genre?}', 'controller#method');
Controller part
function method($fields = null) {
return jsonData;
}
Found a way but its kinda awful...
route
Route::get('books', 'controller#method');
url
link.com/books?genre=crime,programming,love,religion
method
function method() {
$array = explode(',',$_GET['fields');
//.....
return jsonData;
}
there are few ways you can achieve that.
if want to achieve,
http://link.com/books/crime/programming
or
http://link.com/books/crime
you can use the following routes:
Route::get('books/{genre}/{genreOptional?}', function($genre, $genreOptional = null)
{
dd($genre, $genreOptional);
});
or
Route::get('books/{genre}/{genreOptional?}', 'controller#method' );
Your controller:
public function method($genre, $genreOptional = NULL)
{
//
}
I am starting to learn codeigniters active record and i am querying my database using parameters passed from the controller to the model.
First i am passing the id from the controller to the model and that works.
Controller
function bret($id){
$this->load->model('school_model');
$data = $this->school_model->get_city_and_population($id);
foreach ($data as $row)
{
echo "<b>Name Of The City</b>...........". $row['Name'];
echo "<br/>";
echo "<b>Total Population</b>...........".$row['Population'];
}
}
Model
function get_city_and_population($id){
$this->db->select('Name,Population');
$query = $this->db->get_where('city', array('ID'=>$id));
return $query->result_array();
}
I went ahead and put in multiple parameters expecting to fail but this works but i am not so sure why it worked or what worked.
Controller
public function parameters($id,$name,$district){
$this->load->model('school_model');
$data = $this->school_model->multiple_parameters($id,$name,$district);
foreach ($data as $row)
{
echo "<b>Total Population</b>...........".$row['Population'];
}
}
Model
function multiple_parameters($id,$name,$district){
$this->db->select('Population');
$query = $this->db->get_where('city', array('ID'=>$id,'Name'=>$name,'District'=>$district));
return $query->result_array();
}
In my multiple parameters example i visited http://example.com/env/at/index.php/frontpage/parameters/7/Haag/Zuid-Holland/
Here,i know the name Haag is in id 7 and the district is Zuid-Holland
Here are my questions.How does codeigniter know how to pass the parameters from the url to the model and secondly,what if i was slightly wrong like 7/Haag/Zuid-Hollandes/,how would i show the user that,that url is wrong and fallback to a default value instead of showing blank when the parameters are wrong?.
//In codeiginter URI contains more then two segments they will be passed to your function as parameters.
//if Url: http://example.com/env/at/index.php/frontpage/parameters/7/Haag/Zuid-Holland/
//Controller: forntpage
public function parameters($id,$name,$district){
echo $id.'-'$name.'-'.$district;
}
//and if you are manually getting url from segment & want to set default value instead of blank then use following:
public function parameters(
$this->load->helper("helper");
$variable=$this->uri->segment(segment_no,default value);
//$id=$this->uri->segment(3,0);
}
//or
//Controller: forntpage
public function parameters($id='defaultvalue',$name='defaultvalue',$district='defaultvalue'){
echo $id.'-'$name.'-'.$district;
}
That's just simple uri mapping in CI, or uri param binding if you will.
When you have a method like:
public function something($param1, $param2) {
// get from: controller/something/first-param/second-param
}
That means your uri segments are passed as arguments to your controller method.
The above method could be written as:
public function something() {
$param1 = $this->uri->segment(3);
$param2 = $this->uri->segment(4);
// segment 1 is the controller, segment 2 is the action/method.
}
You need to understand that you have to manually check if the uri segments are exactly what you want them to be, as CI doesn't do anything else than this mapping.
Next, if you want to have some defaults, following statement is true:
public function something($param1 = 'some default value', $param2 = 'other value') {
// get from: controller/something/first-param/second-param
}
That is, if a url like: /controller/something is passed along, you will still get your default values back. When controller/something/test is passed, your first param is overridden by the one from the url (test).
That's pretty much it.