Zend Framework-Best way to hide Controller name - url

I am getting one problem in zend framework. I want to hide controller name in my zend framework url.
For Example https://example.com/index/about-us
I want to hide index controller from my url and i want url like
https://example.com/about-us
index is my default controller and about-us is my method
I have tried following code but it doesn't help
i have put this code in /application/Bootstrap.php
public function _initCustomRoute(){
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route(':action', array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('default-override', $route);
}
please help me to solve this problem.

Related

Zf2 ScnSocialAuth HybridAuth zfcuser and Routes

Using ZF2 to customise an Entity based on ZfcUser. Trying to use ScnSocialAuth and got a bit of a problem.
The problem is that I am using custom routes ('/account' instead of '/user') and when implementing ScnSocialAuth I cannot get the social code into my custom zfcuser view...?
I have \\view\zfc-user\user\register.php which overrides the zfcuser registration.
I have a customised route:
'account' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/account',
),
),
These are my zfc config modification within \my-module\config\module.config.php
'zfcuser' => array(
// telling ZfcUser to use our own class
'user_entity_class' => 'WMember\Entity\WMember',
// telling ZfcUserDoctrineORM to skip the entities it defines
'enable_default_entities' => false,
'table_name' => 'w_member',
'login_redirect_route' => 'account',
),
My global \config\application.config.php
'ScnSocialAuth',
'MyModule1',
'ZfcBase',
'ZfcUser',
'BjyAuthorize',
'GoalioMailService',
'GoalioForgotPassword',
'my-user-module',
Therefore, after all this:
I can see my own extended User registration form by navigating to
/account/register with no Social login links visible
I can see the ScnSocialAuth when navigating to /user/register
a) I cannot create the view in my module to override \vendor\scn-social-auth\user\register.phtml as was done with zfcuser
Please help with getting ScnSocialAuth to work with my custom route setup.
If this is just wrong please let me know as I'm not ZF2 expert. Happy to take 'constructive' criticism.
Saw these posts: How to (correctly) extend ScnSocialAuth\Authentication\Adapter\HybridAuth::authenticate() method?
and this as a result of the above post:
https://github.com/SocalNick/ScnSocialAuth/issues/202
NOTE: still running ZF-2.3* due to PHP 5.3,5.4
Instead of adding a custom route to your config, you need to over-ride the zfcuser route
<?php
// #file MyUserModule/config/module.config.php
return array(
// other config ...
'router' => array(
'routes' => array(
'zfcuser' => array(
'options' => array(
// this is the only change needed to route zfcuser to /account
'route' => '/account',
),
),
),
),
// more config ...
);
The ScnSocialAuth module uses the forward() plugin to render the content from zfcusers register view (and login view iirc), which means it will only ever look at the zfcuser route and completely ignore your custom route. The only way to have it use your custom route would be to replace ScnSocialAuths UserController with your own using identical code but forwarding to your custom route (much more work there, and still the potential to break anything else that expects zfcuser to be the route used)

cakephp url modification: remove action and add slug inflector

I'm trying to remove the action in the cakephp url and add a slug inflector, to be more clear this is my expected output:
from this: example.com/posts/view/81/This is a test post
to this: example.com/posts/This-is-a-test-post
This is my current code:
That gives me this output: example.com/posts/view/This is a test post
Controller:
public function view($title = null) {
if (!$title) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findByTitle($title);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}
view.ctp:
$this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['title']));
I also tried this one,this link being my reference CakePHP: Use post title as the slug for view method :
Output: example.com/posts/view/21/This-is-a-test-post
Controller:
function view($id) {
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}
view.ctp
$this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['id'], Inflector::slug($post['Post']['title'],'-')));
below are the other links that I tried but failed, I also tried the modifying the routes.php but can't make the proper code to make it work
want to remove action name from url CakePHP
How to remove action name from url in cakephp?
Remove action name from Url in cakephp?
any help/suggestions is appreciated thanks.
Use Id and Named parameter...
On routes.php
define new routes as-
Router::connect('/posts/:id-:title',
array('controller' => 'posts',
'action' => 'view')
);
This new defined routes will match and parse all url containing id and title named parameter..
Important Note:: Do not define new routes after the end of routes.php. Try to define on the middle of the file..
On view
echo $this->Html->link('Hi',array(
'controller' => 'posts',
'action' => 'view',
'id' => 4,
'title' => Inflector::slug('the quick brown fox')
));
Well the solution provided above will fulfill your needs, but still after reading your question one thing comes into my mind... in your controller you are trying to read posts using title I mean this will slow down your system not recommended in programming so use id and increase one more column in your db table for slug(SEO title) which will be used for creating your post urls.
For example:
Post title: This is a test post
Create seo title for this as: this-is-a-test-post-123
Stor seo title in DB as <this-is-a-test-post>
See 123 is your posts ID now change your controller function to get data based on id ie.123, I hope you cn extract 123 from the sting easily...
Note: remember you have to think about this-is-a-123 string also because
they also land in post having id 123.
Use below route for the above solution:
Router::connect('/posts/*', array('controller' => 'posts', 'action' => 'view'),array('pass' => array('title')));
Now in your controller:
You will get string "this-is-a-test-post-123" in $post_title
function view($post_title=null){
$temp = explode('-',$post_title);
$posts_id= end($temp);
$lastKey = end(array_keys($temp));
unset($temp[$lastKey]);
$seoTitle = implode("-",$temp);
//Now compare the above seoTitle with DB seo title for unique urls
}

zf2 website with a single entry-point, no routes/paths in URL

Is it be possible to make a website that doesn't reveal any relative URL's at all?
Say for example, I have a domain name "somedomain.xyz" and I want to route everything through the default route, and I want not to reveal any paths or route structures to the end user.
The end user shall only see the domain name in the browser's address bar, like:
http://somedomain.xyz
or
https://somedomain.xyz.
Any path like
http://somedomain.xyz/index.php
or
http://somedomain.xyz/index or
http://somedomain.xyz/index/index
shall show a 404.
And I don't care about SEO stuff and static pages.
Is that possible with ZF2, and if yes, then how?
similar question: hide module and action name from zf2 routing
Just create a hostname route for subdomain.xyz like so:
'my-route' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'subdomain.xyz',
'defaults' => array(
'controller' => 'MyApp\Controller\TheController',
'action' => 'whatever-action',
),
),
),
see here for a complete solution, with using HTTP POST vars for the routing:
ZF2 routing via post vars

Html->link in home page to another controller action

I'm trying to link a controller action in my home page. this is what i wrote:
<?php $this->Html->link(__('Assign', array('plugin' => 'full_calendar',
'controller' => 'events',
'action' => 'assign',
$events['Event']['id']
)
)
); ?>
but when the home page renders, the button label is /pages/Assign, and the url is pointing to this path: www.mysite.com/pages/Assing
How can I escape from pages controller and link to another controller action?
You are closing the parenthesis wrong :)
<?php $this->Html->link(__('Assign'), array('plugin' => 'full_calendar',
'controller' => 'events',
'action' => 'assign',
$events['Event']['id']
)
); ?>
The __() function should only correspond to the "Assign" string.
Beside that tiny mistake (always happens, so be sure to check for parenthesis mishaps before panicking), the way you're calling the function is ok and it should redirect you correctly.

Zend Framework 2: Navigation (\Zend\Navigation\Navigation)

I would like build a menu in Skeleton Application (ZF2). I made a separate module Navigation.
At Navigation Controller wrote a code:
namespace Navigation\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Navigation\Navigation;
class NavigationController extends AbstractActionController
{
public function indexAction()
{
$container = new \Zend\Navigation\Navigation(array(
array(
'label' => 'Album',
'controller' => 'album',
'action' => 'index',
'route' => 'album',
)
));
$navigation = new \Zend\View\Helper\Navigation\Menu();
$navigation->renderMenu($container);
return new ViewModel(array('navigation'=>$navigation));
}}
And get the mistake:
Zend\Navigation\Page\Mvc::getHref cannot execute as no Zend\Mvc\Router\RouteStackInterface instance is composed
At file: \vendor\zendframework\zendframework\library\Zend\Navigation\Page\Mvc.php:186
How resolve it?
It seems that the route 'album' is not defined within your Navigation module. Routing needs to be set up in module.config.php of your Navigation module. The route needs to contain an additional parameter :module which points to the Application module, I think.
Click the link
to download ZF2 dynamic navigation from database. There are functionality for add, edit, delete menu items.

Resources