CakePHP admin prefix with dash? - cakephp

I would like my admin prefix to be admin-manage, but later, when how can I define function in my controller?
I can't write function like this :
admin-manage_add(){
//some code here
}
I have added to my core.php this :
Configure::write('Routing.prefixes', array('admin-manage'));
How can I manage this? Thank you.

You can modify the default prefixrouting routes and include them manually in your routes.php.
Router::connect("/admin-manage", array('controller' => 'client','action' => 'index', 'prefix' => 'admin', 'admin' => true));
Router::connect("/admin-manage/:controller", array('action' => 'index', 'prefix' => 'admin', 'admin' => true));
Router::connect("/admin-manage/:controller/:action/*", array('prefix' => 'admin', 'admin' => true));
This way the admin-manage url gets routed to the admin prefix and that is what you use for your controller actions:
admin_add(){
//some code here
}
I think you should to put
Configure::write('Routing.prefixes', array('admin'));
in your core.php though, but I think it doesn't matter anyway.

Related

CakePHP 3 use default routing in some cases

I have a CakePHP 3.5 website. It was necessary to set slugs without any url prefixes like /pages/slug, so I wrote the following rule:
$routes->connect(
'/:slug',
['controller' => 'Pages', 'action' => 'redirectPage']
)
->setMethods(['GET', 'POST'])
->setPass(['slug'])
->setPatterns([
'slug' => '[a-z0-9\-_]+'
]);
It works nice, but in some cases I want cakePHP to route as default (Controller/Action/Params). For example, I want /admin/login to call 'login' action in 'AdminController'.
I have two ideas that doesn't need exact routings, but I can't make any of them to work:
Filtering some strings by pattern: It would be nice if I could filter some strings, that if slug doesn't match pattern, it will simply skip the routing rule.
Create a '/admin/:action' routing rule, but then I cant use :action as an action variable. It causes errors.
$routes->connect(
'/admin/:action',
['controller' => 'Admin', 'action' => ':action']
)
Any ideas?
Thanks
You can use prefix for admin restricted area.
Example:
Router::prefix('admin', function ($routes) {
$routes->connect('/', ['controller' => 'Users', 'action' => 'login']);
$routes->fallbacks(DashedRoute::class);
});
$routes->connect('/:slug' , [
'controller' => 'Pages',
'action' => 'display',
'plugin' => NULL
], [
'slug' => '[A-Za-z0-9/-]+',
'pass' => ['slug']
]);
Now, for example, path /admin/dashboard/index will execute method in Admin "subnamespace" \App\Controller\Admin\DashboardController::index()
Its nicely described in docs: https://book.cakephp.org/3.0/en/development/routing.html#prefix-routing
Try this:
$routes->connect(
'/admin/:action',
['controller' => 'Admin'],
['action' => '(login|otherAllowedAction|someOtherAllowedAction)']
);
Also, your slug routes seems to not catch /admin/:action routes, b/c dash is not allowed there: [a-z0-9\-_]+

Cakephp admin url routing not working

I have developed a large website in CakePHP. On development time my admin URL was SITE_URL/admin. Now my client wants it with lc_admin. So I change prefixes in core.php file but when I tried to access any page it shows me error that lc_admin_index() action is not defined. Because my actions are with admin_index and so on.
To solve this issue I tried code below
Router::connect('/lc_admin/:controller', array('action' => 'index', 'prefix' => 'admin', 'admin' => true));
Router::connect('/lc_admin/:controller/:action/*', array('prefix' => 'admin', 'admin' => true));`
But with this my old URL called SITE_URL/admin also working. And I want new URL accessible.
if you want direct route path by keyword then use simple.
Route path
Router::connect('/lc_admin',array('controller' => 'admin', 'action' => 'index'));
you should give the prefix the same value, as you have in your url, please try this:
Router::connect('/lc_admin/:controller', array('prefix' => 'lc_admin', 'admin' => true));
Router::connect('/lc_admin/:controller/:action', array('prefix' => 'lc_admin', 'admin' => true));
Router::connect('/lc_admin/:controller/:action/*', array('prefix' => 'lc_admin', 'admin' => true));
also, please note, that all your actions (that are called with this url pattern) should have prefix admin_ or you should change it accordingly.

CakePHP - How to make routes with custom parameters?

My Cake URL is like this:
$token = '9KJHF8k104ZX43';
$url = array(
'controller' => 'users',
'action' => 'password_reset',
'prefix' => 'admin',
'admin' => true,
$token
)
I would like this to route to a prettier URL like:
/admin/password-reset/9KJHF8k104ZX43
However, I would like the token at the end to be optional, so that in the event that someone doesn't provide a token it is still routed to:
/admin/password-reset
So that I can catch this case and redirect to another page or display a message.
I've read the book on routing a lot and I still don't feel like it explains the complex cases properly in a way that I fully understand, so I don't really know where to go with this. Something like:
Router::connect('/admin/password-reset/:token', array('controller' => 'users', 'action' => 'password_reset', 'prefix' => 'admin', 'admin' => true));
I don't really know how to optionally catch the token and pass it to the URL.
You'll want to use named parameters. For an example from one of my projects
Router::connect('/:type/:slug',
array('controller' => 'catalogs', 'action' => 'view'),
array(
'type' => '(type|compare)', // regex to match correct tokens
'slug' => '[a-z0-9-]+', // regex again to ensure a valid slug or 404
'pass' => array(
'slug', // I just want to pass through slug to my controller
)
));
Then, in my view I can make a link which will pass the slug through.
echo $this->Html->link('My Link', array('controller' => 'catalogs', 'action' => 'view', 'type' => $catalog['CatalogType']['slug'], 'slug' => $catalog['Catalog']['slug']));
My controller action looks like this,
public function view($slug) {
// etc
}

CakePHP Global slug router

I have configured slugs for all my posts and I need the Router that will do the links like:
/controller/post_slug_name
and I need this for all the controllers, but when I go with:
Router::connect('/admin', array('admin' => true, 'controller' => 'settings', 'action' => 'dashboard'));
Router::connect('/:controller/:slug', array('action' => 'index'), array('pass' => array('slug')));
The admin panel is not working. How can I make it like this, simple, and with admin panel working? Thanks
EDIT:
With those tree routers is working as I would like, except that in the control panel I am getting even the index actions in the urls and not cool
Router::connect('/admin', array('admin' => true, 'controller' => 'settings', 'action' => 'dashboard'));
Router::connect('/admin/:controller/:action/*', array('admin' => true));
Router::connect('/:controller/:slug', array('action' => 'index'), array('pass' => array('slug')));
I have not tried this so I am not sure it works, but please try the following...
Router::connect('/:controller/:slug', array('action' => 'view:slug'));
You view function also needs to accept $slug as paramenter:
function view($slug){
...
}
If the above does not work, you could try this as well:
Router::connect('/:controller/*',array('action' => 'view'));
Once again, I have not tried any of these codes and dont know if any works, just putting ideas outthere. When I get home I will them.
Thanks,
try connecting /admin/* instead of just /admin

Routing: 'admin' => true vs 'prefix' => 'admin in CakePHP

Hi I'm setting up admin routing in CakePHP.
This is my current route:
Router::connect('/admin/:controller/:action/*', array('admin' => true, 'prefix' => 'admin', 'controller' => 'pages', 'action' => 'display', 'home'));
It works fine, but I don't understand what the difference between 'admin' => true, and 'prefix' => 'admin' is.
When I omitted 'prefix' => 'admin', the router wouldn't use admin_index and would instead just use index. So what's the point of 'admin' => true?
By setting 'prefix' => 'admin' you are telling CakePHP that you want to use a prefix of admin for that route; basically meaning you want to use controller actions and views that have names prefixed with admin_. This part you are already aware of, and things will probably work fine with just this.
When creating routes though, any array keys passed into the second argument that aren't recognised by CakePHP (ie. not your usual controller, action, plugin, prefix stuff) are set as named parameters during requests matching that route.
Adding 'admin' => true is therefore just a named parameter in this case, but it comes with its advantages. Firstly, it can make code more succinct.
/* Determine if a request came through admin routing */
// without:
if ($this->params['prefix'] == 'admin') {}
// with:
if ($this->params['admin']) {}
/* Create a link that is reverse-routed to an admin prefixed route */
// without:
$html->link('...', array('prefix' => 'admin', 'controller' => 'users'));
// with:
$html->link('...', array('admin' => true, 'controller' => 'users'));
Secondly, it provides backwards compatibility with the way admin routing worked in CakePHP 1.2 (the last line from the above example is how you would have made admin routing links in 1.2). Therefore, developers migrating from 1.2 to 1.3 can prevent having to change links throughout their application by keeping the 'admin' => true flag in their routes (and adding the 'prefix' => 'admin' one).
Lastly, by setting a custom flag like this with a named parameter and using it in your application instead of referencing your route by an exact string means that you prevent yourself from ever having to change links if you change the prefix to something else (say from admin to administrator or edit)... although this is sort of a moot point, as you would need to rename all your admin_* controller actions and views. :)
// Go into a prefixed route.
echo $html->link('Manage posts', array('manager' => true, 'controller' => 'posts', 'action' => 'add'));
// leave a prefix
echo $html->link('View Post', array('manager' => false, 'controller' => 'posts', 'action' => 'view', 5));

Resources