I'm facing an issue with codeigniter URI routing.
I have a route like this:
website.local/terms/SOMEINFORMATIONID/SOMECURRENCY
Where SOMEINFORMATIONID & SOMECURRENCY are the URI parameters.
The problem is, when I call website.local/terms/SOMEINFORMATIONID/SOMECURRENCY/SDFSDFSDFSD
So this means, that i'm introducing a third parameter, the URI still works, but what I want is, when the route is "executed" with more than 2 parameters, get a 404 error.
I was trying to find something in codeigniter documentation, but I couldn't find anything, anyway I don't know for what I should look for (codeigniter uri parameters limited, etc...).
As additional information, my route looks like this in routes.php:
$route['terms/(:any)/(:any)'] = 'xxx/terms/$1/$2';
in controller:
//check segment
$sdf= $this->uri->segment(4);
if ( ! empty($sdf)) $this->my404();
and function
public function my404 () {
$this->output->set_status_header('404');
$data['error'] = 'Not Found...'
$this->smarty->view( 'error.tpl', $data);
}
if you are using ci3, you can add one more route at last like
$ route ['(. +)'] = 'error_page'; // all the urlĀ“s that you don't have before will display the error you want
Related
I try to get my route without any additional parameter in url and i'm getting Sorry, the page you are looking for could not be found. error.
If I use url like: domain/category/lenovo where lenovo is my slug it's working. But if I try to get my url like: domain/lenovo I'm gettin error above.
here is my codes:
route
Route::get('/{categoryslug}', 'frontend\FrontendController#totalcategoriessubs')->name('catwithsubs')->where('categoryslug', '[\w\d\-\_]+');
function
public function totalcategoriessubs($categoryslug) {
$categories = Category::where('slug','=',$categoryslug)->with('subcategories')->paginate(12);
return view('front.categoriessubs', compact('categories'));
}
it also brakes my other urls such as domain/admin/dashboard etc.
any idea?
It seems that is caused by the order of your routes.
Your actual problem is that when you hit a fixed endpoint, for example /dashboard this will be handled by Route::get('/{categoryslug}', ...) before the correct endpoint (Route::get('dashboard', ...)). So, as your system notices that there is no category with that slug (dashboard) it throws the error.
Try change the order of your routes, like this:
Route::get('/category/{categorySlug}', 'CategoryController#index');
Route::get('/dashboard', 'DashboardController#home');
// and so on..
Route::get('/{categoryslug}', 'frontend\FrontendController#totalcategoriessubs')
->name('catwithsubs')->where('categoryslug', '[\w\d\-\_]+');
Always put the routes that have fixed parts (.../category/...)of the route before the ones that have dynamic elements (.../{categoryslug}/).
I've 2 routes in my web.php
1) Route::get('/{url}', 'MenuController#menu');
which provide url :
/menu
2) Route::get('/{name}', 'HomeSlideviewController#index')->name('promotiondetail');
which provide url :
/menu (different page but same name in route 1)
/food
I want to use 2 route if route = same name I want to use route 1 if route 1 dont have url It will use route 2 . In web.php is their anyway to do something like
if(Route::get('/{url}', 'MenuController#menu')) is null use
`Route::get('/{name}', 'HomeSlideviewController#index')->name('promotiondetail');`
now in my web.php I do this
Route::get('/{url}', 'MenuController#menu');
Route::get('/{name}', 'HomeSlideviewController#index')->name('promotiondetail');
when I go /food It will go page not found.
UPDATE
In my controller I try this
try {
// if find url
}
} catch (\Exception $e) {
//if not find url
return redirect()->route('promotiondetail', $url);
}
and It return Error redirected you too many times
UPDATE 3
$url = food
Your problem is that when you use
Route::get('/{url}', 'MenuController#menu');
Route::get('/{name}', 'HomeSlideviewController#index')->name('promotiondetail');
you are having the same request because {url} or {name} are optional parameters and what happens is that it will always match the first case. The best solution for you can be using this part of code:
Route::get('/menu', 'MenuController#menu');
Route::get('/{name}', 'HomeSlideviewController#index')->name('promotiondetail');
You should always have the one with only optional parameters last, because otherwise it will always be executed first because it will be matching. And what you should remember is that using /{name} it will match anything, it is like a variable and can contain a number also it can be a string, for instance a url might be domain/{anything}. If you use /name it will match only if you are having domain/name as request.
You might want to read Laravel routing for more information about routing.
The problem you have is that both routes are essentially the same, /{something}.
You have a couple of solutions.
Firstly, sort out your routes, make them slightly different so they don't match each other and correct the order.
For example;
Route::get('/promo/{name}', 'HomeSlideviewController#index')->name('promotiondetail');
Route::get('/{url}', 'MenuController#menu')->name('menu');
Another solution that may work for you is to place the promotiondetail route first, and do a check in that for the same name, if not then redirect to the other controller. So in your index function of HomeSlideviewController, try something like;
public function index($name) {
if ($name !== 'whatever you want it not to be') {
return redirect()->route('menu);
}
// continue
}
I don't know much about the routing concept in codeigniter, I want to pass many parameters to a single method as explained in this http://www.codeigniter.com/userguide2/general/controllers.html tutorial page.
In the url I have this
http://localhost/code_igniter/products/display/2/3/4
In my routes.php I have written
$route['products/display/(:any)'] = 'Products_controller/display';
What I thought is it will pass all the parameters (here 2/3/4) to the method 'display' automatically but I am getting 404 page not found error.
In general I want to achieve something like, if the URI is controller/method I want to route to someother_controller/its_method and pass the parameters if any to that method. How can I do it?
In CI 3.x the (:any) parameter matches only a single URI segment. So for example:
$route['method/(:any)/(:any)'] = 'controller/method/$1/$2';
will match exactly two segments and pass them appropriately. If you want to match 1 or 2 you can do this (in order):
$route['method/(:any)/(:any)'] = 'controller/method/$1/$2';
$route['method/(:any)'] = 'controller/method/$1';
You can pass multiple segments with the (.+) parameter like this:
$route['method/(.+)'] = 'controller/method/$1';
In that case the $1 will contain everything past method/. In general I think its discouraged to use this since you should know what is being passed and handle it appropriately but there are times (.+) comes in handy. For example if you don't know how many parameters are being passed this will allow you to capture all of them. Also remember, you can set default parameters in your methods like this:
public function method($param=''){}
So that if nothing is passed, you still have a valid value.
You can also pass to your index method like this:
$route['method/(:any)/(:any)'] = 'controller/method/index/$1/$2';
$route['method/(:any)'] = 'controller/method/index/$1';
Obviously these are just examples. You can also include folders and more complex routing but that should get you started.
On codeigniter 3
Make sure your controller has first letter upper case on file name and class name
application > controllers > Products_controller.php
<?php
class Products_controller extends CI_Controller {
public function index() {
}
public function display() {
}
}
On Routes
$route['products/display'] = 'products_controller/display';
$route['products/display/(:any)'] = 'products_controller/display/$1';
$route['products/display/(:any)/(:any)'] = 'products_controller/display/$1/$2';
$route['products/display/(:any)/(:any)/(:any)'] = 'products_controller/display/$1/$2/3';
Docs For Codeigniter 3 and 2
http://www.codeigniter.com/docs
Maintain your routing rules like this
$route['products/display/(:any)/(:any)/(:any)'] = 'Products_controller/display/$2/$3/$4';
Please check this link Codeigniter URI Routing
In CodeIgniter 4
Consider Product Controller with Show Method with id as Parameter
http://www.example.com
/product/1
ROUTE Definition Should be
$routes->get("product/(:any)", "Com\Atoconn\Product::show/$1");
I'm trying to write an application that takes a parameter from the url and supplies it to the index function in the default controller. Then decides what to load depending on this parameter either a default home page or a custom page.
$route['(eventguide/:any)'] = 'eventguide';
This code works but only if I have the controller in the url like so:
example.com/eventguide/(parameter)
I don't want to include the controller name. So i'm not exactly sure how to route this.
Ideally the url would look like example.com/(parameter), is this possible?
Yes, you're almost there:
$route['(:any)'] = "eventguide/index/$1";
And in your index() method you fetch the parameter:
public function index($parameter = null){
}
$parameter will now contain anything caught by the :any shortcut, which should be equivalent to (\w)+ IIRC
Since this is a catch-all route, be careful to put any other custom route you want before it, otherwise they will never be reached.
For ex., if you have a controller "admin", your route files should be;:
$route['admin'] = "admin";
$route['(:any)'] = "eventguide/index/$1";
If I have a controller called articles, which has a method called view_articles, a user can type in http://example.com/articles/view_articles/some-post and have it return a page.
I have specified a route to be http://example.com/article/post-name. How can I make it so that only the URL specified in the route is visible? Is there a way for articles/view_articles/some-post to show a 404 instead of showing the same page as the route URL?
I am trying to prevent duplication for SEO purposes.
You can always make default routing to a 404 page by correctly defining routes in your routes.php file:
$routes['article/(:any)'] = 'articles/view_articles/$1';
$routes['(:any)'] = 'main/e404';
As stated by CodeIgniter user guide:
Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
So you can basically define all you want to be seen at the beginning of the file and block everything else on the last line.
As for your main(can be any other) controller's 404 method -
function e404() {
show_404();
}
Use $this->uri->segment(n) as part of the URI class inside of view_articles to redirect traffic to your route if the URI contains view_articles as it's second segment.
I'v done that in other tricky way, first of all you should add some code to __construct function in sys/core/Controller.php
you can check whether the requested url is in routes or not by this code
if(!isset($this->router->routes[uri_string()])){
show_404(); // Or whatever you want ...
}