I have my website under Kohana 3.0 which works perfectly with the defaut Route
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'user',
'action' => 'index',
));
When I try to access my website at this address http://127.0.0.1/web/ It loads the url http://127.0.0.1/web/user . It is OK.
But now I want to add the admin directory under the controller. So my web tree looks like this
classes
| controller/
Admin/
dashboard
web.php
| model
I would like to allow the Admin to access the admin's page in a url like this
http://127.0.0.1/admin/dashboard. Where dashboard is the controller under the admin's directory.
I modify the bootstrap file with this
Route::set('admin', '<directory>(/<controller>(/<action>(/<id>)))',
array('directory' => '(admin)'))->defaults(array(
'controller' => 'user',
'action' => 'index',
));
I can access the admin session through http://127.0.0.1/web/admin/dashboard/
But I can't access the default controller that is http://127.0.0.1/web/ . The error Kohana_Request_Exception [ 0 ]: Unable to find a route to match the URI:
I am missing the default access for the controller.
How can I set Route it to make access to my web site either through the link:
http://127.0.0.1/web/
and
http://127.0.0.1/web/admin/dashboard/
EDIT
From the kohana documentation, it is written
In this example, we have controllers in two directories, admin and affiliate. Because this route will only match urls that begin with admin or affiliate, the default route would still work for controllers in classes/controller.
Route::set('sections', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(admin|affiliate)'
))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
Source : http://kohanaframework.org/3.0/guide/kohana/routing#examples
Now I modify my code to
Route::set('default', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(admin)'))
->defaults(array(
'controller' => 'user',
'action' => 'index',
));
but I have this error
Kohana_Request_Exception [ 0 ]: Unable to find a route to match the URI:
when I want to access the default controller like http://127.0.0.1/user/index
This route would translate to: http://127.0.0.1/admin/web, but your Admin folder would need to have user controller inside.
Route::set('default', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(admin)'))
->defaults(array(
'controller' => 'user',
'action' => 'index',
));
If you want the directory to be optional, you'd need to
Route::set('default', '(<directory>(/<controller>(/<action>(/<id>))))',
array(
'directory' => '(admin)'
)
)
->defaults(array(
'directory' => 'admin',
'controller' => 'dashboard',
'action' => 'index',
));
But, in your case, you need multiple routes. Above the "catch all" route, put this:
Route::set('user', 'user(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'user',
'controller' => 'user',
'action' => 'index',
));
Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin',
'controller' => 'dashboard',
'action' => 'index',
));
Route::set('default', '(/<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'index',
'action' => 'index',
));
Try to insert the directory inside ->defaults
Route::set('whatever', 'whatever')
->defaults(array(
'directory' => 'admin',
'controller' => 'user',
'action' => 'index',
));
Related
Trying to call controllers in controllers/api/v1/ folder in browser. It was working on localhost properly but i get a kohana error after moving to server :
if ( ! class_exists($prefix.$controller))
{
throw HTTP_Exception::factory(404,
'The requested URL :uri was not found on this server.',
array(':uri' => $request->uri()) )->request($request);
}
// Load the controller using reflection
$class = new ReflectionClass($prefix.$controller);
Init:
Kohana::init(array(
'base_url' => '/',
'index_file' => FALSE,
));
Here are my routes:
Route::set('api', 'api/v1(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'api/v1',
'controller' => 'admin',
'action' => 'index',
));
Route::set('subsource', 'api/v1/<controller>(/<id>(/<action>))')
->defaults(array(
'directory' => 'api/v1',
'controller' => 'admin',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
Controllers name start with Controller_Api_V1_
Controllers in /controllers/ folder are working properly.
If i understand you, you need a new externel request. Here are the documentation: Requests
I have controllers
--controllers
--Administrator
-Base.php
-Admin.php (extend Base.php)
-controller1.php
-controller2.php
-etc
And my route looks like this
Route::set('administrator', 'Administrator(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'Administrator',
'controller' => 'base',
'action' => 'index',
));
Try to load this controller and i get message Not found
What's wrong?
UPDATE!
class Controller_Administrator_Base extends Controller_Template {
public $template = 'panel/index';
public function action_index(){
echo 'kupakonia';
}
} // End Welcome
All routes. I was trying alot of sugesstions from google and nothing helps, and I write something becouse i cant update this tobic becouse is too much code inside.
Still can't update.
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'read',
'action' => 'index',
));
Route::set('user', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'user',
'action' => 'index',
));
Route::set('administrator', 'administrator(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'Administrator',
'controller' => 'base',
'action' => 'index',
));
Any sugestions?
Ps. still can't update this topic :/ Wtf must be more characters in tekst than in code ?
this is my first asc on stackoverflow. So sorry for this ;)
You have two catch all routes (default and user) in your Bootstrap file. These two routes do exactly the same.
Also, since these routes are catch all and the order of the routes matter, this means if the catch all route is the first it will match all URLs.
Try this:
Route::set('administrator', 'administrator(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'Administrator',
'controller' => 'base',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'read',
'action' => 'index',
));
Another thing, it is usually bad practice to have a catch all route. Try to create routes specific for your Controllers.
Is there any way I can default a route to use action_index and not have to specify it in the url?
ie.
Route::set('user_profile','(<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'public',
'controller' => 'user',
'action' => 'index',
));
To use that I need to specify /users/index/1234
But I'd like to use /users/1234
I tried taking out action from the Route::set() but I ended up with a 404 page.
UPDATE
Now that I have added this route (the top one) my default route doesn't seem to be working now
Route::set('user_profile','(<controller>(/<id>))')
->defaults(array(
'directory' => 'public',
'controller' => 'users', // Note I changed it to plural to match 'users/*' from your url
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'public',
'controller' => 'home',
'action' => 'index',
));
It's as simple as omitting the <action> param from the URL, but still keeping the default value:
Route::set('user_profile','(<controller>(/<id>))')
->defaults(array(
'directory' => 'public',
'controller' => 'users', // Note I changed it to plural to match 'users/*' from your url
'action' => 'index',
));
Note, that unless you have other Route that overrides this behaviour, your user controller will only be able to execute the index action.
Edit
If your users_profile route is only handling /users path then you can set it in the route explicitly:
Route::set('user_profile','users(/<id>)')
->defaults(array(
'directory' => 'public',
'controller' => 'users', // Note I changed it to plural to match 'users/*' from your url
'action' => 'index',
));
This should address the conflicting routes.
How do I correctly setup routes in Kohana 3.3 where the name of my controller and directory are the same as in my example below?
/application/classes/Controller/Admin/Author.php
- admin/author
- admin/author/add
- admin/author/edit
/application/classes/Controller/Admin/Author/Book.php
- admin/author/book
- admin/author/book/add
- admin/author/book/edit
When using the following routes in the specified order, I'm only able to access admin/author{/action}, but not admin/author/book{/action}.
Reversing the routing order gives me access to admin/author/book{/action}, but not admin/author{/action}
Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin',
'controller' => 'Main',
'action' => 'index',
));
Route::set('admin/author', 'admin/author(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin/author',
'controller' => 'Main',
'action' => 'index',
));
You need something like this:
Route::set('admin-author', '<directory>/<controller>(/<action>(/<id>))', array(
'directory' => '(admin|admin/author)',
'action' => '(add|edit|delete|index)'
))
->defaults(array(
'directory' => 'admin',
'controller' => 'author',
));
Also, you can try to check action with regex ^book (not tested).
Another way is to use Route filters.
you need to define for admin/author/book as well
Route::set('admin-author-book', 'admin/author(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin/author',
'controller' => 'book',
'action' => 'index',
));
Route::set('admin-author-book', 'admin/author(/book(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin/author',
'controller' => 'book',
'action' => 'index',
));
The most important thing about routes: Routes are matched in the order they are added. Reversing the order of your routes was correct, but the problem is that your route admin/author also matches admin/author{/action}. What you could do: make the controller and action mandatory:
// Does not match admin/author/add
Route::set('admin/author', 'admin/author/<controller>/<action>(/<id>)')
->defaults(array(
'directory' => 'admin/author',
));
Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin',
'controller' => 'Main',
'action' => 'index',
));
There are several approaches, another one would be to create a "whitelist" of controllers:
// Does not match admin/author/add
Route::set('admin/author', 'admin/author/<controller>(/<action>(/<id>))',
array(
'controller' => '(book|another-controller)'
))
->defaults(array(
'directory' => 'admin/author',
));
Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin',
'controller' => 'Main',
'action' => 'index',
));
I need to create next structure:
+controller
++admin
+++catalog
++++category.php
++++product.php
+++users
and I need to open them by url /admin/catalog/category/action/param
I tried to create route:
Route::set('admin', '(<directory>(/<controller>(/<action>(/<custom_param>))))',array(
'directory' => '(admin/.*)'
))
->defaults(array(
'controller' => 'dashboard',
'action' => 'index'
));
Not tested:
Route::set('admin', 'admin/<directory>/(<controller>(/<action>(/<custom_param>)))',
array(
'directory' => '(catalog|users)'
))
->defaults(array(
'controller' => 'dashboard',
'action' => 'index',
));