Page redirection is not working from BaseController (Codeigniter 4) - codeigniter

I'm trying to make a separate function checkUser insede BaseController. But page redirection is not working inside this function. Even inside __construction() function this is not working also. But when I use it in initController or any other controller function, then this is working. How can I solve this problem, please guide me.
BaseController (In my case AdminBaseController)
This is Dashboard Controller
Showing this error

As you can see in the CI4 documentation, initController() is called after __construct(). Then, when you call checkUser(), your response object is still not initialized, thus null.
You could move the code of your __construct() method into the initController() method, in your AdminBaseController.

Related

Whats the difference between redirect and this in Codeigniter?

I am new in Codeigniter and it's one of the good frameworks of php. But on some conditions I'm confused. Like this one. If any of you have any clarification about my dough, it's a great help for me.
Offcouse redirects refresh the page and $this not but apart from this I want to know - anyhow both of them used to go to somewhere else on view pages or like in other controller or in same controller to other methods.
But we don't use these side by side because when getting any of them it will go to that page or method without checking the next lines.
In case of a normal difference then have lot's of but I just want to know about the condition of going to next page or method when we use redirect or $this like this -
$this->Function($value); //It's method of same controller.
redirect('Controller/function'); //It's also doing same with page reload.
Thank for looking my problem.
Redirect()
When you will call any function of helper in codeigniter then you can call function directly without using any object. Helper in Codeigniter is collection of functions.
Redirect() method is a part of URL helper in Codeigniter.
For your ref. https://www.codeigniter.com/user_guide/helpers/url_helper.html
So, just load helper using $this->load->helper('url'); or you can also mention in autoload.php file.
$this->Function(); used to call a function from same controller
$this->Function(); used to call a function from same controller
redirect()
While building a web application, we often need to redirect the user from one page to another page. CodeIgniter makes this job easy for us. The redirect() function is used for this purpose.
redirect($uri = '', $method = 'auto', $code = NULL)
The first argument can have two types of URI. We can pass full site URL or URI segments to the controller you want to direct.
The second optional parameter can have any of the three values from auto, location or refresh. The default is auto.
The third optional parameter is only available with location redirects and it allows you to send specific HTTP response code.
Redirect means jumping to another function mentioned in the redirect method.
$this->Function($value); => jumping to another function and you can execute the code of the same function as well as pass the value back by returning value.
When you send request to codeigniter generally CI controller gets called and then function which is mentioned in uri segment. like below... So this will be another request.
redirect('Controller/function'); //It's also doing same with page reload.
But when you have to call another function within the same request then you can use below approach
$this->Function($value); //It's method of same controller.
This will execute the given function and return the value within same request.

codeigniter routing general understanding

I tried to understand how the routing in CodeIgniter work.
I want to use normal access to sides which are not a kind of user area or something special - only normals links in the main root of the website.
So I try this.
I've set in the routes.php
$route['/'] = "index";
I've created controller names Frontend.php and a model named Frontend_modell.php
The Controller (nothing to do)
public function index()
{
redirect(site_url('index'));
}
The Modell
public function __construct()
{
parent::__construct();
}
I've added an index.php inside the view-folder Frontend and I add the index.php (for test) in the main folder from appilation and in the view folder.
If I try to access www.domain.com I see the URL will change to www.domain.com/index, but no side will come up. "the page you requested was not found."
What I do wrong ? I hope somebody can explain to me how it works correctly and why.
First of all routes uses controller_name/method_name and here you tried to use method_name directly.
Secondly, most of cases you don't need to play with routes unless you need a special handler or rerouting, so mostly keep it to default.
Btw the url rerouted to www.domain.com/index cause it looked for a controller named index as you specified and there in no controller found with that name it should be frontend if that's your controller.
The reason for the problem was from another point.
The Controller i've create had some bugs, after checking the function of the CR Controller, the routing works fine :-))

Same route on first link it works, but on second not. Laravel 5.6

Route file web.php:
Route::get('/download/received/{image_id}/{isoriginal?}', 'DownloadController#download_recv_image');
View:
<li>Download {{strtoupper($image->extension)}}</li>
<li>Download PNG</li>
Function in controller:
public function download_recv_image($image_id, $original=false){...}
This is function for download received image. When I click on first link in view route is called and function is executed. But on second link where I'am not sending second parameter then it returns me error 404 and it looks like it cant catch route.
(I have another function for download user images, with same logic for route definition in another two links and there everything works.)
I have found where the problem is.
That's because above that route I have another route called:
Route::get('download/{image_id}/{isoriginal?}', 'DownloadController#download_user_image');
I have changed second route to /received/download instead of /download/received
It's messing up because both routes have the same beginning and parameters ar messed up.

How do I declare a controller method for a named route in Laravel 4?

I am attempting to declare a method/function in my controller that responds to a numerically named route. When I load any page in the site I receive an error stating the controller method could not be found, meaning that Laravel won't even load the application do to some incorrect formatting. I searched for an answer with no luck.
Here is the route I'm attempting to access via my Math controller:
students/academics/math/7-12
Here is the method declaration to look for the route:
public function get712()
Which gives me the following error no matter what page I'm loading:
Call to undefined method Illuminate\Routing\Router::get712()
I'm not sure how to name the function/method in my controller for a purely numeric routes since hyphen is not allowed and there is no upper/lowercase for numbers.
why not pass 7-12 as a variable to a method?
route:
students/academics/math/{number}
controller:
public function getMath($number)
{
// code here
}
I remembered that Laravel used to use underscores in the method name instead of camelCase so after scouring Google with no luck I declared the method like this:
public function get_7_12()
voila!

Attempting to use _remap to create page controller

I'm trying to create a default page controller for a site with urls like http://www.example.com/about. So, if there isn't an 'about' controller, then go looking for a page with that url string in the database.
I've set the 404_override to my 'page' controller and am using the _remap() function to determine where to go next, either load the homepage (the page controller's index() method) or load a page.
However, as it stands right now if I use a different controller (product, in this case) with a method in the path (http://www.example.com/product/widget, which doesn't exist) I am being served the index() method of the page controller.
I thought the problem comes from the way I'm checking if the requested page is the homepage in my _remap() so I added an echo to the beginning of my remap to see if it mattered. The _remap() doesn't appear to be called. Instead, in attempting to handle the 404_override CI just shows the index method.
Any ideas how I can accomplish this more effectively?
public function _remap($method)
{
echo 'Method '.$method;
if($this->uri->segment(1) == null)
{
// $this->index();
} else {
$this->view();
};
}
I seem to have found a workable solution in this blog post: http://pinoytech.org/blog/post/codeigniter-route-everything-except-these-controllers
Using a regular expression in the route definition allows for other controllers (product, order, etc.) to be ignored while still sending everything else to the page controller.
$route['^(?!product|order|misc_controller)\S*'] = "page/$1";
Haven't tested it out just yet, but as far as I can tell it should work swimmingly.

Resources