New Routes in Codeigniter - 404 Page Not Found - php

I am working on an existing project on CodeIgniter and there was a need to create new routes. Existing routes work correctly, but new ones give a 404 error. Even when I make a copy of the route and controller, I also get a 404 error.
$route['offer/list'] = "offer/list_offers";
$route['offer/list1'] = "offer/list_offerss";
Second is copy.

For an example.
After you added the routes, always create the Controller after that and call the view.
Note :
Don't use my $this->loadviews()...
Use the default
$this->load->view('phpNameWithoutTheExtension');
I use $this->loadviews() because i have another libraries that controll the view.

Related

Laravel Routing Parameter Contraint

Been working on a neat little project. I'm using Laravel 5.5, and I'm building routes to handle the various requests. I've got a route that accepts a slug to locate a particular guild via route model binding. Works great! Beautifully, actually. Then, I defined a "static" route that doesn't use a parameter to show the form for creating a new guild. Here's the routes...
Route::get('/guilds', 'GuildController#index')->name('guilds');
Route::get('/guild/{guild}', 'GuildController#show')->name('guild');
Route::get('/guild/create', 'GuildController#create')->name('create_guild');
Route::get('/guild/{guild}/edit', 'GuildController#edit')->name('edit_guild');
Route::post('/guild/create', 'GuildController#store')->name('store_guild');
But when I attempt to navigate to '/guild/create', I get a 404 because a guild with the slug "create" doesn't exist. How can I work around this particular issue?
Try put specific routes first:
Route::get('/guilds', 'GuildController#index')->name('guilds');
Route::get('/guild/create', 'GuildController#create')->name('create_guild');
Route::post('/guild/create', 'GuildController#store')->name('store_guild');
Route::get('/guild/{guild}', 'GuildController#show')->name('guild');
Route::get('/guild/{guild}/edit', 'GuildController#edit')->name('edit_guil');

access controller inside the controller directory within another folder

This is my controller directery structure
controllers
-----user(folder)
----------User_reg.php //My Controller inside user folder.
-----index
-----Welcome.php
I can access User_reg.php like this:
http://localhost/coin_system/user/user_reg
Now I want to eliminate user from this URL so I added the route
$route['user_reg/(:any)'] = 'user/user_reg/$1';
But it show the error: 404 Page Not Found
I want to access it like this
http://localhost/coin_system/user_reg
How can i access the controller inside the directory in controller?
I have tried to solve using this SO question but it did't help.
I am using Codeigniter latest version. 3.1.5
You have missed function
https://www.codeigniter.com/user_guide/general/routing.html#examples
$route['user_reg'] = 'user/user_reg/index';
$route['user_reg/(:any)'] = 'user/user_reg/index/$1';
Or You can have different function
$route['user_reg'] = 'user/user_reg/somefunction'
$route['user_reg/(:any)'] = 'user/user_reg/somefunction/$1';
Also try with index.php in url
http://localhost/coin_system/index.php/user_reg
When you add route with :any it also find your calling method after controller. But for index(which is default) its not compulsory to all index method So you need to specify route for it also. So just need to add one more route
$route['user_reg'] = 'user/user_reg'; // add this to route file
$route['user_reg/(:any)'] = 'user/user_reg/$1';

Routing in CodeIgniter for (:any)

I'm trying to make my CodeIgniter application work similarly to WordPress.
I want to be able to make these kind of URLs:
http://www.example.com/my-post-example
http://www.example.com/new-headline-here
http://www.example.com/i-love-stackoverflow
My routing:
$route['(:any)'] = "core/index/$1";
Which will call my Core controller and pass the page name into the index function.
I then lookup in my database for the page name and display the page to the user. So far so good.
However, there will be times when I want to call another controller. For example:
http://www.example.com/admin/edit_page/3
http://www.example.com/admin/settings
Now I assume my route will just grab all these rules and send them into my Core controller. Is there a way to make an exception for certain pages? Or is it a good idea to do this check inside my Core controller.
For example,
if ($page not in DB) {
// Call controller/method
}
This seems a little redundant since I just want CodeIgniter to handle this.
The routing rule you using it is OK for your purpose.
If you use http://www.example.com/admin/edit_page/3 this link it will send you admin controller and edit_page method.It will not use routes any rule.
However you will get one problem if your link looks like this
http://www.example.com/my-post-example/test
It will try to go my-post-example controller and test method.
Again http://www.example.com/admin will use routes any rule, means it will redirect your to core controller instead of admin/index. In that case your url should be http://www.example.com/admin/index
Finally If you call your other link with controller/method name it will be OK using your any rule

Laravel 5 Error Pages

I'm having problems creating error pages for my L5 app. I created a controller called BaseController which has all my CSS/JS and every other controller I have extends from it. How does one create an error 404 page which also extends BaseController?
Simply creating a view in views/errors/404.blade.php does not work since no styles are loaded. I'm using Twigbridge which is really useful when working with views.
Am assuming when you say controller has CSS/JS it means that your setting up layout using controllers.
Please note that this works for routes that you clearly define in your routes file and their respective controllers extend the Base.
For Error Pages they are not handle by your controllers. The error handlers are responsible for rendering their views and won't extend any layout. Hence you need to complete define it in your error view.
This should get you off on the right foot. Just use your standard extends syntax to extend your "default" layout.
Do a search for App::error, you will find the right spot.
App::error(function(Exception $exception, $code)
{
$params = array();
$request = Request::create('cms/noroute', 'GET', $params);
return Route::dispatch($request)->getContent();
});
There is no need to handle the errors within a controller or even the assets files. Imagine you have an error from laravel (missing a config file) so you will have the error before event the code could reach a controller. The same case goes for 404. If you do not have that route available, there is no need to let user reach a controller, this is handled by Exception Handler.
A good resource to learn more is Laravel documentation and this blog post

How to check if Controller already exists when already configured route using Codeigniter?

On my website, I am loading the content dynamically from database like this
e.g mysite.com/about-us
for this, there is an enrtry in database, so it will load the content for 'about-us' & print it using "page" controller only.
for this what I have done is, I have added below configuration in routes.php
$route[':any'] = "page";
but lets say if I already have controller named "about-us" and I want to load that & not the one from database, how can I do that?
A smooth solution would be to use the error/missing_page controller and point it in the config/routes.php.
Then it would automaticly pick all existing controllers first, and then that controller.
You can also call show_404() if you don't find a record in the database.
This allows you to create new controllers without having to point all of them in the route file.
Read about 404 override here
you need to add this
$route['about-us'] = "aboutus";
$route['about-us/(:any)'] = "aboutus/$1";
before
$route[':any'] = "page";
as the CI route is not greedy, it will not check for the page controller after it finds the about-us controller.

Categories