Laravel 5 routing not working when using slashes inside groups? - url-routing

I am facing a little weird problem with a simple routing in Laravel 5. Below, I have to commented code snippets.
// It works when I call /tribut.updateStatus URL
$router->group(['prefix' => 'admin', 'before' => 'isLogged|isAdmin'], function ($router) {
$router->resource('tribut', 'Admin\TributController');
$router->get('tribut.updateStatus', 'Admin\TributController#updateStatus');
});
// Does not work when I call: /tribut/updateStatus URL
$router->group(['prefix' => 'admin', 'before' => 'isLogged|isAdmin'], function ($router) {
$router->resource('tribut', 'Admin\TributController');
$router->get('tribut/updateStatus', 'Admin\TributController#updateStatus');
});
I want to use the second route option. Is it possible? What am I doing wrong? When I call the routing that does not work, the screen goes blank. No error is shown, neither in log files.

The problem is that $router->resource('tribut') registers a route that caches everything with GET tribut/* because it thinks * is an id.
The solution is pretty simple, just define the explicit get route before the resource route:
$router->group(['prefix' => 'admin', 'before' => 'isLogged|isAdmin'], function ($router) {
$router->get('tribut/updateStatus', 'Admin\TributController#updateStatus');
$router->resource('tribut', 'Admin\TributController');
});

Related

Cakephp 3.6 - Missing Controller Error

I am having this error which does not seem to make sense. I am using this plugin: https://github.com/hashmode/cakephp-tinymce-elfinder. I am in need of creating and admin route. However, even though CakePHP sees the plugin, it does not see the Controller within it. I don't see what I am doing wrong.
This is my route for /admin/elfinder:
Router::prefix('admin', function ($routes) {
$routes->connect('/elfinder', ['plugin' => 'CakephpTinymceElfinder', 'controller' => 'Elfinders', 'action' => 'elfinder']);
});
This is the controller/action I am trying to access
https://github.com/hashmode/cakephp-tinymce-elfinder/blob/master/src/Controller/ElfindersController.php
But I get the following error:
2018-06-01 15:20:33 Error: [Cake\Routing\Exception\MissingControllerException] Controller class Elfinders could not be found.
Request URL: /admin/elfinder
It is definitely finding the Plugin. Why can't CakePHP find the controller?
According to the official CookBook you need to configure your prefixed routes in the following way. Hope this helps.
Router::plugin('YourPlugin', function ($routes) {
$routes->prefix('admin', function ($routes) {
$routes->connect('/:controller');
});
});
https://book.cakephp.org/3.0/en/development/routing.html#prefix-routing
Some time ago I wrote a plugin for my personal needs. I needed to bind its controllers to the /shop and /shop/api URLs. I managed to do it like this
Router::scope('/shop',['plugin'=>'MyPlugin'] ,function (RouteBuilder $routes) {
$routes->prefix('api', function($routes) {
$routes->extensions(['json']);
$routes->connect('/:controller');
$routes->resources('Categories');
$routes->resources('Products');
$routes->resources('Prices');
$routes->resources('Pricetypes');
});
$routes->connect('/:controller');
$routes->fallbacks(DashedRoute::class);
});

Koa.js render angular html page

I was trying to use Koa.js instead of express for node.js.
In express we have used render function to fetch the html page.
I tried to access angular html page from Koa.js using the below code as below:
app.use(async function (ctx, next) {
return send(ctx, 'views/index.html', { root: ''
})
.then(() => next()) })
But the above code displays the index page as it is without styles and the ng-view datas are not rendered
Also, I tried adding co-views as below
var views = require('co-views');
var render= views(__dirname + '/views',
{ map: { html: 'swig' }});
But, I didnt get the result as expected. It also displays the page as mentioned above.
Please help to get the expected result.
Unless you have very specific requirements for serving static files, most of the time you can get away with koa-static:
import serve from 'koa-static'
// ...
app.use(serve(`${__dirname}/public`))
If you have more particular needs, you might need to edit your question.

CakePhp Routing Interfering with AngularJS Routes

Okay so the problem I am having deals with using CakePhp (v3) and AngularJS. I want to use CakePhp purely to host my JSON webservices. I have this working and the routes prefixed with API. The problem I have is getting Html 5 routing to work with Angular. For instance, if I go to http://localhost/appname/home I want Angular to intercept this route not CakePhp. Currently, if I were to do this CakePhp will complain that the HomeController doesn't exist. I found this stack overflow post which allowed me to get Angular Hashtag routing functional/while maintaining the /api/* routes, but I really don't like to have my routes look like http://localhost/appname/#/home. I feel like there should be an .htaccess change I could make to allow Cake to handle the default URL path (ie: http://localhost/ thus opening by default home.ctp) but otherwise only look for http://localhost/api/* prefixed routes. I did try adding a RewriteRule ^/api/* with no luck unfortunately. I typically don't work in Apache/PHP in general so I am struggling to find the solution to this problem. Also, I made sure the rewrite module is enabled in my httpd.conf file. Here is my current routes.php file showing the API prefixing and the default routes to bring up home.ctp when hitting the root of the host. Any guidance from a more seasoned *AMP developer is much appreciated!
<?php
use Cake\Core\Plugin;
use Cake\Routing\Router;
Router::defaultRouteClass('Route');
Router::prefix('api', function ($routes) {
$routes->extensions(['json', 'xml']);
$routes->resources('Users');
$routes->resources('UserInfo');
Router::connect('/api/users/register', ['controller' => 'Users', 'action' => 'add', 'prefix' => 'api']);
Router::connect('/api/userinfo/create', ['controller' => 'UserInfo', 'action' => 'add', 'prefix' => 'api']);
$routes->fallbacks('InflectedRoute');
});
Router::scope('/', function ($routes) {
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
$routes->fallbacks('InflectedRoute');
});
Plugin::routes();
Maybe I'm misunderstanding your question, but by the time the request gets to the server, wouldn't it already be too late? If you want "angular to intercept" the route, the solution would need to be done client side and not in a .htaccess file. So you would have angular route that routes /appname/home to the correct file.

CakePHP Routing query

How do I show the URL through CroogoRouter::connect function in CakePHP?
I passed the name of controller and action my routes file like:
CroogoRouter::connect('/', array('admin' => true, 'controller' => 'dashboard', 'action' => 'index'));
and I get redirected to the dashboard page, but I do not see the URL properly. I want my URL as localhost/abc/admin/dashboard and it is showing as localhost/abc only.
Since you are using the webroot (/) as route alias, it will not show the admin/dashboard bit. If you really want that (I'd discourage it if it's just for fancy display purposes), you should create a simple controller action that redirects. For example if you alter the route to this:
CroogoRouter::connect('/', array('controller' => 'dashboard', 'action' => 'home'));
And then in the DashboardsController create this action:
public function home() {
$this->autoRender = false; // We have no view, so don't render anything
$this->redirect(array('admin' => true, 'controller' => 'dashboard', 'action' => 'index'));
}
It should display the URL as you want it. Again, if it's only for display purposes (to make it look "nice") and not for any SEO kind of purpose, I'd discourage using such an ugly workaround. But it should do the trick either way.
Can you please check the controller name i think controller name should be dashboards or something.
check this link may be helpful http://bakery.cakephp.org/articles/Frank/2009/11/02/cakephp-s-routing-explained

Url Rewriting of once controller - cake 2.0

I'm developing a simple CMS in CakePHP, right now it has 4 controllers in it(Menus,Site,Roles,Users), I want to rewrite one controller, but I'm having problem.
I use all the actions only as admins for admin purpose like admin_view, admin_add......
except siteController(this controller is only for frontend purpose)
I need my www.example.com/site/view/something_here must be replaced to www.example.com/something_here - this will be displayed in front-end so.
I added a line in my routes file:
Router::connect('/*', array('controller' => 'site', 'action' => 'view'));
But after adding this I couldn't able to use other controllers.
I again added some more lines before the above line:
Router::connect('/admin/Menus/*', array('controller' => 'menus', 'prefix' => 'admin'));
Same for all other controllers, but if I send any action or id in url it doesn't works.
like - http://www.exmple.com/admin/menus/[view/1] - the one inside square bracket doesn't works.
any Ideas on rewriting this?
I just answered a similar question on another thread.
To put the admin controller routes before the '/*'-route was the right idea, but the way you did it the router can't assign an action. You could use the following for each controller:
Router::connect('/admin/Menus/:action/*', array('controller' => 'menus', 'prefix' => 'admin'));
Or you could use the default prefix-routing routes, so you don't have to add a route for each new controller.
// prefix routing default routes with admin prefix
Router::connect("/admin/:controller", array('action' => 'index', 'prefix' => 'admin', 'admin' => true));
Router::connect("/admin/:controller/:action/*", array('prefix' => 'admin', 'admin' => true));

Resources