How to Do backend and Frontend architectures in cakephp 3.0? - frontend

How separate the backend and frontend (controllers, view, layout) in the same application share the models in CakePHP 3 ?

if you use bin/cake bake in your terminal, you can add --prefix=Backend
ex controller: bin/cake bake controller NameOfYourTable --prefix=Backend
ex template: bin/cake bake template NameOfYourTable --prefix=Backend
CakePHP will create the subfolder ./src/Controller/Backend/NameOfYourTable
with the good namespace namespace App\Controller\Backend; and ./src/Template/Backend/NameOfYourTable/ with index.ctp, add.ctp, edit.ctp, view.ctp
And add the prefix your urls in routes.php
ex url: www.domain.tld/backend/nameofyourcontroller/
Router::prefix('backend', function($routes) {
$routes->connect(
'/',
['controller' => 'NameOfYourController', 'action' => 'index']
);
$routes->connect(
'/:controller',
['action' => 'index'],
['routeClass' => 'InflectedRoute']
);
$routes->connect(
'/:controller/:action/*',
[],
['routeClass' => 'InflectedRoute']
);
});

Related

How to add common model to Twig and Slim4

I'm using Twig and Slim4 with DI container (the same as this tutorial: https://odan.github.io/2019/11/05/slim4-tutorial.html).
I would like to know how can I add a common model to all my twig views, for example user object, general options and something like this.
This is the container Twig initialization:
TwigMiddleware::class => function (ContainerInterface $container) {
return TwigMiddleware::createFromContainer($container->get(App::class), Twig::class);
},
// Twig templates
Twig::class => function (ContainerInterface $container) {
$config = $container->get(Configuration::class);
$twigSettings = $config->getArray('twig');
$twig = Twig::create($twigSettings['path'], $twigSettings['settings']);
return $twig;
},
The twig middleware is the Slim standard one: Slim\Views\TwigMiddleware
You can add global variables to Twig environment, so they are accessible in all template files:
(To be able to provide a sample code, I assumed you have defined a service like user-authentication-service which is capable of resolving current user)
// Twig templates
Twig::class => function (ContainerInterface $container) {
//...
$twig = Twig::create($twigSettings['path'], $twigSettings['settings']);
$twig->getEnvironment()->addGlobal(
'general_settings',
[
'site_name' => 'my personal website',
'contact_info' => 'me#example.com'
]);
$twig->getEnvironment()->addGlobal(
'current_user',
// assuming this returns current user
$container->get('user-authentication-service')->getCurrentUser()
);
return $twig;
},
Now you have access to general_settings and current_user in all of your template files.

How to add meta tags on custom paths in Drupal 8?

Does anybody know how can I add meta tags on custom paths in Drupal 8? I am using the metatags module.
Thank you!
You can add any meta tag programmatically from your custom module using hook_page_attachments() as below.
function MYMODULE_page_attachments(array &$page) {
$ogTitle = [
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:title',
'content' => 'This is my page title',
],
];
$page['#attached']['html_head'][] = [$ogTitle, 'og:title'];
}
I've tested succesfuly Metatag Routes module for Drupal 8. The module adds a button in the default metatag dashboard page. The user can configure metatags for custom controllers generated by code.
Another way to make it in a controller :
public function index()
{
$variables = [];
return [
'#theme' => 'my_theme',
'#variables' => $variables,
'#attached' => [
'library' => [
],
// Added for open graph and SEO.
'html_head' =>
[
[
'#tag' => 'meta',
'#attributes' => [
'name' => 'og:descritpion',
'description' => 'My sublim description',
],
],
'og:descritpion',
],
];
}
The only solution actually with metatag module for Drupal 8, is to use fields in your content type, and use them as metatag element with token possibilities.
It works well like that. I also wait for a better solution.

kohana dynamic uri routing to redirect to a controller function

I have to write a route in kohana for the following
$this->request->redirect("/" . input::post('custom_url'));
example if the custom_url = testpage
then it should be 'abc.com/testpage' and it should be redirected to a controller function example contoller/profile.
example if the custom_url = testpage1
then it should be 'abc.com/testpage1'
Route::set('event', '(event(/<url>))')
->defaults(array(
'controller' => 'event',
'action' => 'profile',
));
my url will be domainname/event/customname and it should redirect to event/profile. where event is controller and profile is action

Using Silex Firewall in SecurityServiceProvider with {_locale}

When I configure Silex SecurityServiceProvider to work with {_locale} param, login.check_path returns a LogicException as follow:
LogicException: The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?
Following is my settings:
$app->register(new Silex\Provider\SecurityServiceProvider, [
'security.firewalls' => [
'login' => [
'pattern' => '^/{_locale}/login',
'security' => false,
],
'games' => [
'pattern' => '^/{_locale}/modules/',
'form' => [
'login_path' => '/{_locale}/login',
'check_path' => '/{_locale}/modules/login_check',
],
'logout' => [
'logout_path' => '/{_locale}/modules/logout',
],
...
],
...
],
]);
login_path and firewall handling seem to work fine, but can't finalize login process.
What's wrong with it?
I also ask you what's the correct route name of login_path, check_path and logout_path to serve to Twig {{ path() }} method, as I can't figure it out due to {_locale} presence.
Thank you.
Had the same problem today :) and found this solution:
add default_target_path and always_use_default_target_path to your security.firewalls form config.
'form' => array(
'login_path' => '/',
'check_path' => 'login_check',
'default_target_path' => '/' . $app['locale'],
'always_use_default_target_path' => true
),
Edit
So using $app['locale'] doesn't work well.
It's better you use $app['session']->get('locale') and set locale to session in a before middleware
$app->before(function (Request $request) use ($app) {
$app['session']->set('locale', $app['locale']);
....
});
Maybe somebody has a better solution by overloading event listeners. But I think this is a quick solution.

how to remove action name from URL in cakephp2

may be duplicate but I don't get any proper answer or help
actually I want to do like:
my current URL is : http://mysite.com/MyController/view/page1
but I want something like :http://mysite.com/MyController/page1
means I want to hide action name from URL.
I have used
Router::connect('/:controller/:id',array('action' => 'view'),array('id' => '[0-9]+'));
but its not working for me
below one working fine but
Router::connect('/:controller/*', array('action' => 'view'),array('id' => '[0-9]+'));
it applies for all controller but I wanted to apply for specific controller
use
Router::connect('/MyController/:id', array('controller' => 'MyController','action' => 'view'),array('id' => '[0-9]+'));
You can use Cake's routing to get this to work.
Add the following to your app/Config/routes.php
Router::connect('/Controller/page1', '/Controller/view/page1');
But you will have to add a route for every 'page'.
You can use a wildcard route to match everything starting with /Controller/:
Router::connect('/Controller/*', '/Controller/view/');
Or, without touching routes:
class FooController extends AppController
public function index($stub) {
$data = $this->findByStub($stub);
if (!$data) {
die('page not found');
}
$this->set('data', $data);
}
}
}
Which allows you to have urls such as /foo/page1
(The routine looks for a Foo with a stub field matching 'page1')
This works, but you will loose the benefit of reverse routing which means you can make links like this: $this->Html->link(array('controller'=>'foo', 'action'=>'view', 'page1'); which cake will automagically rewrite to produce: /foo/page1
use this code
Router::connect('/:controller/*', array('action' => 'view'),array('id' => '[0-9]+'));
Try the following code for your controller, Here am using GroupsController as an example
You add this in your app\Config\routes.php
Router::connect('/groups/:slugParam', array('controller' => 'groups', 'action' => 'index'), array('slugParam' => '[a-zA-Z0-9]+'));
This should redirect all requests of the form
http://www.site.com/groups/* to http://www.site.com/groups/index
( * => whatever comes after the controller name )
So now i have to alter my default index function in GroupsController to reflect this change
<?php
App::uses('AppController', 'Controller');
class GroupsController extends AppController {
public function index($id = null) {
//pr($this->request->params); this where all data is intercepted...
if(isset($this->request->params['slugParam']) && !empty($this->request->params['slugParam'])) {
// i have a slug field in groups databsae and hence instead of id am using slug field to identify the post.
$data = $this->Group->findBySlug($this->request->params['slugParam']);
$this->set('group', $data);
$this->render('/groups/view');
}
}
}
?>
use this code
Router::connect('/MyController/:id', array('controller' => 'MyController','action' => 'view'),array('id' => '[0-9]+'));

Resources