CodeIgniter: Page not found when passing parameters to a controller? - php

I'm trying to pass parameters to a control in codeigniter, but I'm getting 404 page not found error, I don't get it, I did what the guide says: http://codeigniter.com/user_guide/general/controllers.html#passinguri
When I remove the params in the index function and just access the controller everything works fine, but I can't pass a value to it...
Here is the code the way I'm trying to send a param:
http://mysite/123
<?php
class Main extends Controller {
function index($username) {
echo $username;
}
}
?>
How can I get more info regarding this error from codeigniter?
Thank you.

With that URL, CodeIgniter can't understand if you want to pass 123 to the index method or if you're requesting the 123 method with no parameters. You have to explicitly name the default method if you need to pass it some parameters.
http://mysite/index/123

Option 1 - Rempap the function call in your controller
If your controller contains a function named _remap(), it will always get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which function is called, allowing you to define your own function routing rules.
http://codeigniter.com/user_guide/general/controllers.html#remapping
Option 2 - Use a custom route.
http://codeigniter.com/user_guide/general/routing.html

Related

default value for route parameter in Laravel

Hi My website must be multi-lang and everything are OK except one piece for example my homepage route is like below:
Route::get('/{lang}','Welcomecontroller#index');
and in AppServiceProvider in boot method I write function that came in following:
public function boot()
{
$request=Request::capture();
Cookie::queue('lang',$request->lang);
$lang=$request->has('lang') ? $request->lang : "fa";
app()->setLocale($lang);
}
problem of my code is here,my code works fine when pass query string like ?lang=en
but in passing parameter in route like get('/{lang}','WelcomeController#index'); if parameter is not exist
404 page showing and always showing fa lang if parameter pass. Is there any way to this method if {lang} parameter isn't exist by default showing for example en lang and url showing just like http://localhost:8000/ against http://localhost:8000/en and how get {lang} parameter from url in that method.
You can make it nullable like that
Route::get('/{lang?}','Welcomecontroller#index');
And inside your controller Welcomecontroller in index function you can pass default value like that:
public function index($lang = 'fa'){
// code
}
you can get more information from https://laravel.com/docs/6.x/routing#parameters-optional-parameters
you can use URL::defaults.
also you can get your answer from https://laravel.com/docs/5.6/urls#default-values

How to use the request route parameter in Laravel 5 form request?

I am new to Laravel 5 and I am trying to use the new Form Request to validate all forms in my application.
Now I am stuck at a point where I need to DELETE a resource and I created a DeleteResourceRequest for just to use the authorize method.
The problem is that I need to find what id is being requested in the route parameter but I cannot see how to get that in to the authorize method.
I can use the id in the controller method like so:
public function destroy($id, DeletePivotRequest $request)
{
Resource::findOrFail($id);
}
But how to get this to work in the authorize method of the Form Request?
That's very simple, just use the route() method. Assuming your route parameter is called id:
public function authorize(){
$id = $this->route('id');
}
You can accessing a Route parameter Value via Illuminate\Http\Request instance
public function destroy($id, DeletePivotRequest $request)
{
if ($request->route('id'))
{
//
}
Resource::findOrFail($id);
}
Depending on how you defined the parameter in your routes.
For my case below, it would be: 'user' not 'id'
$id = $this->route('user');
Laravel 5.2, from within a controller:
use Route;
...
Route::current()->getParameter('id');
I've found this useful if you want to use the same controller method for more than one route with more than one URL parameter, and perhaps all parameters aren't always present or may appear in a different order...
i.e. getParameter('id')will give you the correct answer, regardless of {id}'s position in the URL.
See Laravel Docs: Accessing the Current Route
After testing the other solutions, seems not to work for laravel 8, but this below works
Route::getCurrentRoute()->id
assuming your route is
Route::post('something/{id}', ...)
I came here looking for an answer and kind of found it in the comments, so wanted to clarify for others using a resource route trying to use this in a form request
as mentioned by lukas in his comment:
Given a resource controller Route::resource('post', ...) the parameter you can use will be named post
This was usefull to me but not quite complete. It appears that the parameter will be the singular version of the last part of the resource stub.
In my case, the route was defined as $router->resource('inventory/manufacturers', 'API\Inventory\ManufacturersController');
And the parameter available was manufacturer (the singular version of the last part of the stub inventory/manufacturers)
you will get parameter id if you call
request()->route('id')
OR
$this->route('id')
if you're using resource routing, you need to call with the resource name
// eg: resource
Route::resource('users', App\Http\Controllers\UserController::class);
$this->route('user')
in Terminal write
php artisan route:list
to see what is your param name
Then use
$this->route('sphere') to get param

Routing in codeigniter

I am confused with codeigniter routing. I am implementing URL masking in my project by using router in codeigniter.
From this I got confusion about the routing.routes has given below.
$route['project/shareToFacebook/(:any)']="project/shareToFacebook/$1";
$route['project/shareToFacebook/(:any)']="project/profile/$1";
My question is when I called the controller shareToFacebook what does route will do?
whether controller profile will be invoke or the controller shareToFacebook will be invoke?
Note: Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
CodeIgniter user guide: Routing
You will always be sent to shareToFacebook, but you will be sent to any of these routes only if you pass some parameters so when you will call the controller it will open it's index method regardless, if you won't pass any arguments.
when I called the controller shareToFacebook what does route will do?
The method 'shareToFacebook' will accept 1 parameter ex.
if you call ex. localhost/yourproject/profile/shareToFacebook/1 <- will be passed on the method shareToFacebook
public function shareToFacebbok($value)
and you can do what ever you want with that value.
whether controller profile will be invoke or the controller shareToFacebook will be invoke?
No.
profile will still call its index method

What is required to call a controller in Codeigniter?

I'm creating a simple blog with Codeigniter. But I'm having trouble calling another controller besides the default controller.
The following URL takes me to the default controller as specified in my config/routes.php.
blog/index.php
According to the documentation, simply appending the name of another controller saved in controllers/ is all that is needed:
blog/index.php/blog_login
Here is my controller class, named blog_login.php:
class Blog_login extends CI_Controller {
public function index()
{
echo 'It works!';
}
}
But doing this throws a 404 error, which makes me feel that I'm missing something. Is there something else that I am supposed to configure before trying to access a different controller?
http://codeigniter.com/user_guide/general/routing.html Read this properly, it couldn't be clearer.
According to the documentation, simply appending the name of another
controller saved in controllers/ is all that is needed
This is not true. If you want to call another controller 'Blog_login', you simply put the name of the controller as the first segment of the url:
domain.com/index.php/blog_login
This will never work:
blog/index.php/blog_login
Index.php (unless you remove it via .htaccess) always follows right after your domain.com
Finally, you don't need to specify routes unless you're doing something non standard. So
domain.com/index.php/blog_login - calls the index() function in your Blog_login controller
domain.com/index.php/blog - calls the index() function in your blog controller
domain.com/index.php/blog/search - calls the search() function in your blog controller.
None of the above examples need an entry in routes.php
When u call:
blog/index.php/blog_login
you're really calling a method called "blog_login" in your "blog" controller. If you want to call another controller, it must have the following structure:
controller_name/controller_method
So, if you wanna call your blog_login controller just call it like this:
blog_login/
Note: Sometimes it's necessary to add the base_url() to your URL in order to make CI understand correctly the URL.

Custom routing in code ingniter

I want to use codeigniter for an ecommerce project I'm working on but I think I need some custom routing and I'm not sure if this is possible. I want to be able to use this url:
http://myecommsite.com/store/mens
By default in CI this would call the mens function in the store class. What I actually want is it to call a generic function in the store class and feed in 'mens' as a paremeter. The reason for this is that this site needs to have a mens,womens and childrens section.
Is this possible?
Also When I get further down the line...i.e
http://myecommsite.com/store/mens/category1/category2
how do I make Ci work with this?
Simply define a custom route in application/config/routes.php
Something like, for your url http://myecommsite.com/store/mens
$route['store/(:any)'] = "store/customfunction/$1";
This way all requests will be mapped to your "customfunction" method, which takes the parameter "mens"
You might also want to consdere the __remap() function, which overrides the methods (as opposed to the routing, which overrides the whole URI) Quoting from the manual:
If your controller contains a function named __remap(), it will always
get called regardless of what your URI contains. It overrides the
normal behavior in which the URI determines which function is called,
allowing you to define your own function routing rules.
So you can use a __remap() function in your controller store, and anything will be redirected to that. Any segments after the method name are passed into __remap() as second parameter, and you can use this array with call_user_func_array().
This could come in handy for your second examples of URI. Might be something like
function __remap('mymethod',$array = array())
{
return call_user_func_array('mymethod',$array);
}
and in your method "mymethod" you pick the array element and do what you need to do

Categories