Passing Optional parameters not calling to the correct one - php

Little stuck with Parameters, market and sort are optional
Route::get('Category/{title}/{market?}/{sort?}', 'HomeController#productList');
But when i do this in the URI
url: Category/title/?sort=3
it doesn't register as 3 for sort but goes in as the market paramater
of course if the URI was Category/title/Makert/3 it will return what i want
public function productList($title, $market = null, $sort = null)
{
// Gets the Categories with its Markets
$categorys= Product::cats();
$brands = Product::CategoryWithMarket($title, $market)->groupBy('Brand')->get();
$subCat = Product::where('Category', $title)->groupBy('Market')->orderBy('Market', 'ASC')->get();
if (!$market) {
$marketList = Product::where('Category', $title)->orderBy('Brand', 'ASC')->orderBy('Label', 'ASC')->paginate(15);
$brands = Product::where('Category', $title)->groupBy('Brand')->orderBy('Brand', 'ASC')->get();
$mainTitle = ucfirst($title);
}
else {
// Gets the list of products with the catery and market attributes arranged by the brand of product
$marketList = Product::CategoryWithMarket($title, $market)->paginate(15);
$mainTitle = ucfirst($title) . ' ' . ucfirst($market) ;
}
return $sort;
In theroy it should pass back the sort parameter which is 3 but it doesnt return anything, so my question is how do i get sort to return its value 3 rather than being null

Router only uses the path to match the routes and only path parameteres are injected to the controller. So if you you want sort to be passed to the controller from the router you need to put that in the path (/{sort?}), not in the query (?sort=3).
If you want to access query parameters in your controller, you can do this via $request object (if it's your action's argument) or Request facade:
public function someAction() {
echo Request::query('sort');
}

Related

PHP/Laravel, url with multiples arguments

I'm building a Laravel app, and I need to use an URL that looks like that :
/api/ads?page=Actuel&formatsQuery[]=side&formatsQuery[]=leaderboard&deviceQuery=mobile
I have 3 parameters (page, formatsQuery (as an array), and deviceQuery).
Do you now how to hold his in routing and controller in order to have the correct value inside the controller's fonction?
I tried this :
routes/api.php
//request to get ads for given parameters
Route::get('/ads', [MediaController::class, 'findAds']);
and this (MediaController.php) :
public function findAds($page, $formatsQuery, $deviceQuery) {
echo $page;
if(sizeof($formatsQuery) <= 0 || sizeof($formatsQuery) > 3){
return $this->unvalidParametersError();
}
//transform format to position depending on deviceQuery
$position = [];
$res = [];
foreach ($formatsQuery as $format) {
$res = Media::where('position', $format)->inRandomOrder()->first()->union($res);
}
echo $res;
return $res;
}
then I test it with this :
public function test_findAds()
{
$ads = Ad::factory()
->has(Media::factory()->count(3), 'medias')
->count(3)->create();
$response = $this->get('/api/ads?page=Actuel&formatsQuery[]=side&formatsQuery[]=leaderboard&deviceQuery=mobile');
$response->assertStatus(200);
}
You are using a GET request to fetch your data. GET request is a type of request that you send parameters in URL using ? after URL and separating parameters with &. You can find out more about HTTP methods here.
In laravel using request parameters is so simple. First of all you need to add Request $request to your method prototype like this:
use Illuminate\Http\Request;
public function findAds(Request $request)
Then you can simply use $request->parameter to get the values. So you need to change your code like this:
public function findAds(Request $request){
$page = $request->page;
$formatsQuery = $request->formatsQuery;
$deviceQuery = $request->deviceQuery;
// Your code
}
And as #matiaslauriti mentioned in the comments you don't need to put [] after formatsQuery[] to send an array in GET request. Using the same key more than one time automatically makes an array for you.

Codeigniter - routes multiple parameters

I want to show single blog news by slug but I do not know
Blog Controller :
public function show_news($slug)
{
$page_data['page_title'] = 'News';
$page_data['news_item'] = $this->blog_model->get_news($slug);
$this->template->load('frontend/blog_news',$page_data);
}
Blog Model :
function get_news($slug)
{
$slugs = urldecode($slug);
$query = $this->db->get_where('blogposts', array('slug' => $slugs));
if($query->num_rows() > 0 ){
if($this->db->get_Where('blogposts', array('slug'=>$slugs))->row()->status == '1'){
return $query->row_array();
}
}
}
my route :
$route['blog/(:any)/news/(:any)'] = "blog/show_news/$1/$2";
you have only one parameter($slug) with show_news function so obviously the route "blog/show_news/$1/$2"; will be incorrect.Manage your route like this..
$route['blog/news/(:any)'] = "blog/show_news/$1";
It redirects every blog/show_news/slug to blog/news/slug
In your controller, you have only 1 parameter. This is not ok with your route configuration. It should be
$route['blog/news/(:any)'] = "blog/show_news/$1"; as already said.
But, your question title is
Codeigniter - routes multiple parameters
If you mean that you want to pass another parameter, than, with your current route, you can access the $2 variable by adding a second parameter to the method of your controller like this.
public function show_news($slug, $secondParameter){

Laravel 4 Route Paramater use only some on given links

I understand that when you do this in Laravel:
Route::get('news/read/{year}/{month}/{date}/{title}/{id}', 'PageController#index_page');
We can use all {var} name as parameters in the controller. But how if I only want to use {id} and {title} instead of all of them in the controller?
This is currently my controller:
public function index_page($year=null, $month=date, $date=null, $title=null, $id=null) {
$plugin_files = $this->addJqueryPlugin(array('unslider'));
$data['css_files'] = $this->addCSS(array('styles'));
$data['js_files'] = $this->addJS(array('main'), false);
$data['css_plugin'] = $plugin_files['css_files'];
$data['js_plugin'] = $plugin_files['js_files'];
if (is_null($id)) {
$data['title'] = 'Homepage';
$this->layout->content = View::make('page.home', $data);
}
else {
$data['isModal'] = true;
$data['title'] = ucwords(str_replace("-", " ", $title . '--' . $id));
$this->layout->content = View::make('page.home', $data);
}
}
I tried putting only $title and $id but it reads from {year} and {month} instead. Only solution I can think of is change the order of the route to news/read/{title}/{id}/{year}/{month}/{date}, but I'm trying to keep the format like the previous one, is it possible?
Firstly, this seems wrong
public function index_page($year=null, $month=date, $date=null, $title=null, $id=null)
Remember that the order of default parameters must be as last parameters of function - check PHP Manual example here for details. I assume you misspelled $month=date for $month='some_default_date_value' ?
Second, answering your questions, you've got at least 2 options here:
A. routing to different methods for different parameter count or order
//Routes
//different routes for different params
Route::get('news/read/{year}/{month}/{date}/{title}/{id}', 'PageController#indexFull');
Route::get('news/read-id/{id}/{title}', 'PageController#indexByIdAndTitle');
Route::get('news/read-some-more/{month}/{date}/{id}/{title}/{year}', 'PageController#indexByWeirdParamsOrder');
//Controller
//different methods for different routes
public function indexByIdAndTitle($id, $title){ return $this->indexFull($id,$title); }
public function indexFull($id, $title, $year=null, $month=null, $date=null) { ... }
public function indexByWeirdParamsOrder($month, $date, $id, $title, $year) { ... }
B. changing the parameters order in route and using optional param / default value
//Routes
Route::get('news/read/{id}/{title}/{year?}/{month?}/{date?}', 'PageController#indexFull');
//Controller
public function indexFull($id, $title, $year=null, $month=null, $date=null) { ... }
Last but not least, check the Laravel docs for routing and parameters.

How does codeigniter know how to pass parameters from controller to the model

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.

Sending $data Variable From Model To View

Okay, so I have this snippet of code in a controller. However, it's all DB driven and should really be in model - I get that. However, as you can see in the IF statement, I need to pass along $data to my view. Based on the outcome. I tried pasting this chuck of coding in a method in my model (calling the model method via controller), however the $data[update_prompt] string is not getting called by the view...
How would I translate this code into a model - sending the $data values back to my controller to embed in my view?
// show appropriate upgrade message if user has free account
$id = $this->session->userdata('user_id');
$this->db->select('subscription'); // select the subscription column
$this->db->where('id', $id); //find id in table that matches session id
$query = $this->db->get("subscriptions"); // connect to this database
$subscribe = $query->result_array(); //returns the result of the above
if($subscribe[0]['subscription'] == 'freebie') // if subscription column equals 'freebie' in the $subscribe array, do this:
{
$data['update_prompt'] = $this -> load -> view('shared/upgrade_subscription', '', TRUE); // adds view within view, $update_prompt
}
else
{
$data['update_prompt'] = '';
}
You would add a function in your model, like so:
public function myModelFunction($id) {
//we return row as we are looking up by primary key and are guaranteed only one row
return $this->db->select('subscription')
->where('id', $id)
->get('subscriptions')
->row();
}
Then, in your controller:
public function myControllerFunction() {
$subscribe = $this->my_model->myModelFunction($this->session->userdata('id'));
if($subscribe->subscription == 'freebie') // if subscription column equals 'freebie' in the $subscribe array, do this:
{
$data['update_prompt'] = $this -> load -> view('shared/upgrade_subscription', '', TRUE); // adds view within view, $update_prompt
}
else
{
$data['update_prompt'] = '';
}
}

Categories