Codeigniter passing multiple parameters issue - php

My function in users class:
public function form($a = false, $b = false, $c= false)
{
// Something to do
}
My request uri:
..admin/users/form/1/2/3
I'm getting 404 error:
404 Page Not Found
The page you requested was not found.
But If i try alphabetical characters like admin/users/form/1/something/1 instead of numeric 2 or 1 places, it works.
So;
..admin/users/form/1/2 > works
..admin/users/form/1/2/3 > not work
..admin/users/form/a/2/3 > works
..admin/users/form/1/a/3 > works
..admin/users/form/1/2/a > not work
And i tried with custom routes and remapping but again i couldnt figure out the issue.

Have you tried
$route['admin/users/form(/:any)*'] = 'admin/users/form';
Then use uri segments in your controller:
public function form()
{
$a = $this->uri->segment(4);
$b = $this->uri->segment(5);
$c = $this->uri->segment(6);
}
I'm not sure why your initial setup isn't working because I always use routes this way. Works fine for me.

Related

Calling custom class method with a parameter in laravel route

So I have this function that is going to be used 1 as an artisan command or 2 as a link a user can use.
Here's the function:
public function saveAll($whatLocation)
{
$userSaved = $this->saveUsersData();
$componentSaved = $this->saveComponentsData();
$issueSaved = $this->saveIssuesData();
$timelogSaved = $this->saveTimelogsData();
if($userSaved === 1 && $componentSaved === 1 && $timelogSaved === 1)
{
$this->allSaved = 1;
}
if($whatLocation != "artisanCommand")
{
return redirect()->back();
}
else
{
return $this->allSaved;
}
}
The idea is that if it's coming from an artisan console command I created I pass "artisanCommand" but if it's coming from a route it pulls my api data and re-directs to the previous page.
Here's the problem:
in the web routes file I now can't call a function that has a parmater?
Route::get('/pull-and-save-api-data', [App\Classes\SaveApiData::class, 'saveAll'])->name('pull-and-save-api-data');
This works if there's no paramater on the function, but will not work now that there's a parameter??
I've tried:
Route::get('/pull-and-save-api-data', [App\Classes\SaveApiData::class, 'saveAll("notcommand")'])->name('pull-and-save-api-data');
Route::get('/pull-and-save-api-data/{notcommand}', [App\Classes\SaveApiData::class, 'saveAll'])->name('pull-and-save-api-data');
neither work?
How about you just make a default argument for the function since you don't have a value to pass to it from the route?
public function saveAll($whatLocation = null)
I wouldn't be calling your method directly from a route like that; have the route go to a Controller and have that Controller interact with the class/lib you need.
If you really had to have a value passed into it from the route you can set a default value:
Route::get('/pull-and-save-api-data', [App\Classes\SaveApiData::class, 'saveAll'])
->name('pull-and-save-api-data')
->defaults('whatLocation', 'notArtisan');

How to fix "Method Illuminate\Database\Eloquent\Collection::links does not exist."?

First I use pagination and I want to change to all in my Controller, but I got the error above.
This is what I tried :
$c = CarModel::all();
My code :
public function index()
{
//$c = CarModel::all(); //got error when use this
$c = CarModel::paginate(3);
return view('car.car',compact('c'));
}
I want to change to $c = CarModel::all(); but I got error.
Looks like you are using ->links() method in your "car.car" view, this method is only available when you use pagination

Laravel function with optional parameter

In my web file, I have a route that accepts a $id as a value to be passed to a function within my PagesController. However, I want the function to still execute and show the intended form even when the $id is not passed to the function.
web.php file
Route::get('/request/{id}', 'PagesController#makeRequest');
PagesController.php file
public function makeRequest($id)
{
if(!empty($id)){
$target = Partner::find($id);
}
return view('pages.makeRequest')->with('target', $target);
}
makeRequest.blade.php
<input type="text" class="form-control" value="{{$target->inst_name}}" required disabled>
I want the page to display details from the database with the $id when provided or have empty spaces when the $id isn't provided.
As the Laravel Documentation states: Use Optional Parameters like this:
Route::get('/request/{id?}', 'PagesController#makeRequest'); //Optional parameter
Controller
public function makeRequest($id = null)
{
if(!empty($id)){
$target = User::find($id);
return view('pages.makeRequest')->with('target', $target);
} else {
return view('pageslist'); ///set default list..
}
}
This is the way I did it:
Route::get('index', 'SeasonController#index');
// controller
public function index(Request $request )
{
$id= $request->query('id');
}
The way you call it:
localhost/api/index?id=7
All your solutions were helpful. The main thing was that when I called just the view without passing $target to the view, the page displayed an error. So this is what I did.
Route::get('/request/{id?}', 'PagesController#makeRequest');
Then in the controller,
public function makeRequest(Request $request, $id=null)
{
if ($id != null) {
$target = Partner::find($id);
return view('pages.makeRequest')->with('target', $target);
}
return view('pages.makeNullRequest');
}
If you didn't understand what happened, I created a new view which had this instead of what I had posted in the question.
<input type="text" class="form-control" value="" required readonly>
Sorry I didn't update you guys in time. I think Jignesh Joisar came closest to helping me solve this issue. really appreciate all you guys. You're just awesome
You can use optional parameter :
Route::get('/request/{id?}', 'PagesController#makeRequest');
Now, as the parameter is optional, while defining the controller function you need to assign its default value to null in argument declaration.
<?php
public function makeRequest($id = null)
{
if($id){
$target = Partner::findOrFail($id);
return view('pages.makeRequest')->with(compact('target'));
}
// Return different view when id is not present
// Maybe all targets if you want
$targets = Partner::select('column1', 'column2')->get();
return view('pages.all')->with('targets');
}
I am using findOrFail instead of find. Its Laravel's very handy function which automatically throws a ModelNotFound exception and for frontend user throws a simple 404 page.
So if anyone is accessing www.url.com/request/2, its a valid id then it will show a valid page with data. If the accessed url is www.url.com/request/blahblah then it will throw 404. It avoids efforts of handling this manually.
For optional parameter pass id with ? in route and give $id = null in your function's parameter like this:
Route::get('/request/{id?}', 'PagesController#makeRequest'); //Optional parameter
makeRequest($id = null) {
// Code here...
...
}
in your routes file (web.php , as mentioned in your question)
Route::get('/request/{id?}', 'PagesController#makeRequest');
and in your controller PagesController.php
public function makeRequest($id = null)
{
}
To read more about this, just read https://laravel.com/docs/5.7/routing#parameters-optional-parameters
For me the answer was in the order that I listed the Routes in the routes file.
The routes file will call the first one that matches the pattern.
Route::get('/ohmy/{id?}', 'OhMyController#show');
Route::get('/ohmy/all', 'OhMyController#all'); //never gets called
Instead, put optional parameters at end of list:
Route::get('/ohmy/all', 'OhMyController#all');
Route::get('/ohmy/{id?}', 'OhMyController#show');
the answer has been said. just a side note: optional parameters won't work if you are using resource routes.
for example:
Route::resource('items',itemController::class)->except([
'create',
]);
Route::get('/items/create/{category_id?}',function($category_id = 'abc'){
dd($category_id);
});
if i go to " items/create/1 ", the result will be "1".
if i go to " items/create ", it will return 404. ( but we expect it to say "abc".)
this happens because other routes that start with "items" are expected to be generated from "resource" functionality.
so if you use resource routes, you should consider that.

Codeigniter route not working for some links

My main concern is what i'm missing here. The route worked perfectly for what i've tested, after i started to add content on the website it stopped working
$route['projects/view/([A-Za-z0-9])/task/([A-Za-z0-9])'] = 'Projects/view_project_task/$1/$2';
Function in model is defined like:
public function view_project_task($project_id, $project_task_id)
If i access it like http://mydomain/projects/view_project_task/1/8 is working perfectly fine, if i try to access like http://mydomain/projects/view/1/task/8 i get a 404 error on some linkes and it works on others.
if you try using callbacks in your route?
$route['projects/(.+)'] = function ($params)
{
$return = explode('/', $params);
//here you need to count how many params has to know if your url
//is like 1/8 -> [0]=>view_project_task [1]=>1 [2]=>8
//or like 1/task/8 [0]=>view [1]=>1 [2]=>task [3]=>8
return 'projects/view_project_task/' . strlower($return[1]) . '/' . ( count($return > 3)) ? $return[3] : $return[2];
};

Getting GET "?" Variable in Laravel

Hello I'm creating an API using REST and Laravel following this article.
Everything works well as expected.
Now, I want to map a GET request to recognise a variable using "?".
For example: domain/api/v1/todos?start=1&limit=2.
Below is the contents of my routes.php :
Route::any('api/v1/todos/(:num?)', array(
'as' => 'api.todos',
'uses' => 'api.todos#index'
));
My controllers/api/todos.php :
class Api_Todos_Controller extends Base_Controller {
public $restful = true;
public function get_index($id = null) {
if(is_null($id)) {
return Response::eloquent(Todo::all(1));
} else {
$todo = Todo::find($id);
if (is_null($todo)) {
return Response::json('Todo not found', 404);
} else {
return Response::eloquent($todo);
}
}
}
}
How do I GET a parameter using "?" ?
Take a look at the $_GET and $_REQUEST superglobals. Something like the following would work for your example:
$start = $_GET['start'];
$limit = $_GET['limit'];
EDIT
According to this post in the laravel forums, you need to use Input::get(), e.g.,
$start = Input::get('start');
$limit = Input::get('limit');
See also: http://laravel.com/docs/input#input
On 5.3-8.0 you reference the query parameter as if it were a member of the Request class.
1. Url
http://example.com/path?page=2
2. In a route callback or controller action using magic method Request::__get()
Route::get('/path', function(Request $request){
dd($request->page);
});
//or in your controller
public function foo(Request $request){
dd($request->page);
}
//NOTE: If you are wondering where the request instance is coming from, Laravel automatically injects the request instance from the IOC container
//output
"2"
###3. Default values
We can also pass in a default value which is returned if a parameter doesn't exist. It's much cleaner than a ternary expression that you'd normally use with the request globals
//wrong way to do it in Laravel
$page = isset($_POST['page']) ? $_POST['page'] : 1;
//do this instead
$request->get('page', 1);
//returns page 1 if there is no page
//NOTE: This behaves like $_REQUEST array. It looks in both the
//request body and the query string
$request->input('page', 1);
###4. Using request function
$page = request('page', 1);
//returns page 1 if there is no page parameter in the query string
//it is the equivalent of
$page = 1;
if(!empty($_GET['page'])
$page = $_GET['page'];
The default parameter is optional therefore one can omit it
###5. Using Request::query()
While the input method retrieves values from entire request payload (including the query string), the query method will only retrieve values from the query string
//this is the equivalent of retrieving the parameter
//from the $_GET global array
$page = $request->query('page');
//with a default
$page = $request->query('page', 1);
###6. Using the Request facade
$page = Request::get('page');
//with a default value
$page = Request::get('page', 1);
You can read more in the official documentation https://laravel.com/docs/5.8/requests
We have similar situation right now and as of this answer, I am using laravel 5.6 release.
I will not use your example in the question but mine, because it's related though.
I have route like this:
Route::name('your.name.here')->get('/your/uri', 'YourController#someMethod');
Then in your controller method, make sure you include
use Illuminate\Http\Request;
and this should be above your controller, most likely a default, if generated using php artisan, now to get variable from the url it should look like this:
public function someMethod(Request $request)
{
$foo = $request->input("start");
$bar = $request->input("limit");
// some codes here
}
Regardless of the HTTP verb, the input() method may be used to retrieve user input.
https://laravel.com/docs/5.6/requests#retrieving-input
Hope this help.
This is the best practice. This way you will get the variables from
GET method as well as POST method
public function index(Request $request) {
$data=$request->all();
dd($data);
}
//OR if you want few of them then
public function index(Request $request) {
$data=$request->only('id','name','etc');
dd($data);
}
//OR if you want all except few then
public function index(Request $request) {
$data=$request->except('__token');
dd($data);
}
Query params are used like this:
use Illuminate\Http\Request;
class MyController extends BaseController{
public function index(Request $request){
$param = $request->query('param');
}
In laravel 5.3 $start = Input::get('start'); returns NULL
To solve this
use Illuminate\Support\Facades\Input;
//then inside you controller function use
$input = Input::all(); // $input will have all your variables,
$start = $input['start'];
$limit = $input['limit'];
In laravel 5.3
I want to show the get param in my view
Step 1 : my route
Route::get('my_route/{myvalue}', 'myController#myfunction');
Step 2 : Write a function inside your controller
public function myfunction($myvalue)
{
return view('get')->with('myvalue', $myvalue);
}
Now you're returning the parameter that you passed to the view
Step 3 : Showing it in my View
Inside my view you i can simply echo it by using
{{ $myvalue }}
So If you have this in your url
http://127.0.0.1/yourproject/refral/this#that.com
Then it will print this#that.com in you view file
hope this helps someone.
It is not very nice to use native php resources like $_GET as Laravel gives us easy ways to get the variables. As a matter of standard, whenever possible use the resources of the laravel itself instead of pure PHP.
There is at least two modes to get variables by GET in Laravel (
Laravel 5.x or greater):
Mode 1
Route:
Route::get('computers={id}', 'ComputersController#index');
Request (POSTMAN or client...):
http://localhost/api/computers=500
Controler - You can access the {id} paramter in the Controlller by:
public function index(Request $request, $id){
return $id;
}
Mode 2
Route:
Route::get('computers', 'ComputersController#index');
Request (POSTMAN or client...):
http://localhost/api/computers?id=500
Controler - You can access the ?id paramter in the Controlller by:
public function index(Request $request){
return $request->input('id');
}

Categories