When working with symfony2, I'd like to show different layouts (base.html.twig for guests, and layout.html.twig for logged users).
And when a user is connected to his account, he can also (As facebook does) "connect as" a page or a group (always inside his account), and for these two I have to show different layouts as well (pagelayout.html.twig and grouplayout.html.twig).
It seems that the solution is to create a controller (indexAction for example), containing all the conditions over the sessions and database queries.
For example (I changed the code for simplification):
public function indexAction(){
// ...
if (!$mySession) { //the user is not connected
//redirection to home controller that uses base.twig.html
} else { //the user is logged in
if ($connectAs=='profile') {
//redirection to home controller that uses layout.twig.html
} else if($connectAs=='page') {
//redirection to page controller that uses pagelayout.twig.html
} else if($connectAs=='group') {
//redirection to group controller that uses grouplayout.twig.html
}
}
}
I wanted to use these conditions in one redirecting controller, but I think it's not a flexible solution. For I can integrate other types of layouts and controllers, I may need to add more conditions.
Is there a better solution, Than can be more flexible?
Edit : Another problem with this solution, is that even if a user is connected as a "Page" , he can still type in the URL of the route to the Group or User controller and get access to them too... unless I added conditions in these controllers too.
What I need exactly, is creating a controller that acts like a frontController
If you want a frontcontroller like logic you need to catch all possible routes with one controller and then forword from there.
# all other routed efinitions should go above this,
# so that _frontcontroller acts like a fallback
_frontcontroller:
path: /{url}
defaults: { _controller: YourFrontBundle:Front:index }
requirements:
name: ".+" #allow / in the route
And your controller should then forward to the controller based on conditions and url
public function indexAction($url)
{
// ...
} else if ($connectAs=='group') {
$this->forward('YourGroupBundle:Group:index');
// or something else based on the url parameter
}
// ...
}
Also there is the CMF Bundle which has dynamic routing but i didn't digg into that yet.
Related
I created Phalcon PHP app,
I have 3 different user profiles: (ID: 1) Administrators, (ID: 2) Accountants and (ID: 3) Warehouses.
I want my app able to render views based on those profiles, for example
controllerName/index.1.volt //for Administrators
controllerName/index.2.volt //for Accountants
controllerName/index.3.volt //for Warehouses
but when those files weren't found, my app will fallback to:
controllerName/index.volt
How do I accomplish that?
One approach, although messy, would be to use controlerName/index.volt as the "landing page" then from there check an if-statement to decide what the user's role is. Then from the if-statement use a partial like {{ partial("index.1.volt") }} but you'd need to hard-code this for every controller... yuck...
A good solution which I'd recommend, though, would be to make use of the View's exists method to check if the view exists from within your controller. The idea is you pass this method the string path to the view file you're looking for but omitting the extension. The reason you omit the extension is because Phalcon allows you to use multiple rendering engines so an application using a mixture of .volt and .phtml would work.
Assuming you were using user roles something like this:
define('GUEST_ROLE',0);
define('ADMIN_ROLE',1);
define('ACCOUNTANT_ROLE',2);
define('WAREHOUSE_ROLE',3);
(with the guest role with a value of 0) I would suggest having all your controllers extend a ControllerBase then define the following two methods in your ControllerBase:
public function getUserLevel()
{
if($this->session->has('userLevel'))
{
$userLevel=$this->session->get('userLevel');
return (int)$userLevel;
}else{
return 0;//default to guest
}
}
protected function initialize()
{
$controllerName=$this->dispatcher->getControllerName();
$actionName=$this->dispatcher->getActionName();
$userLevel=$this->getUserLevel();
if($this->view->exists("$controllerName/$actionName.$userLevel"))
{
$this->view->pick("$controllerName/$actionName.$userLevel");
}
//No reason to add an else, Phalcon defaults to "$controllerName/$actionName"
}
Just make sure, that if you ever need to define a custom "initialize" method for a specific controller which extends the ControllerBase, e.g. to add a title prefix to all pages related to the controller, that you call parent::initialize(); otherwise it won't get called. But that's only if you're going to be overriding the method.
Chances are you're already using a ControllerBase and doing similar logic already, if so, you'd need to edit your already existing "initialize" method and merge my code with yours.
Happy coding.
There will be several high profile links for customers to focus on, for example:
Contact Us # domain.com/home/contact
About the Service # domain.com/home/service
Pricing # domain.com/home/pricing
How It Works # domain.com/home/how_it_works
Stuff like that. I would like to hide the home controller from the URL so the customer only sees /contact/, not /home/contact/. Same with /pricing/ not /home/pricing/
I know I can setup a controller or a route for each special page, but they will look the same except for content I want to pull from the database, and I would rather keep my code DRY.
I setup the following routes:
Route::get('/about_us', 'home#about_us');
Route::get('/featured_locations', 'home#featured_locations');
Which work well, but I am afraid of SEO trouble if I have duplicate content on the link with the controller in the URL. ( I don't plan on using both, but I have been known to do dumber things.)
So then made routes like these:
Route::get('/about_us', 'home#about_us');
Route::get('/home/about_us', function()
{
return Redirect::to('/about_us', 301);
});
Route::get('/featured_locations', 'home#featured_locations');
Route::get('/home/featured_locations', function()
{
return Redirect::to('/featured_locations', 301);
});
And now I have a redirect. It feels dumb, but it appears to be working the way I want. If I load the page at my shorter URL, it loads my content. If I try to visit the longer URL I get redirected.
It is only for about 8 or 9 special links, so I can easily manage the routes, but I feel there must be a smart way to do it.
Is this even an PHP problem, or is this an .htaccess / web.config problem?
What hell have I created with this redirection scheme. How do smart people do it? I have been searching for two hours but I cannot find a term to describe what I am doing.
Is there something built into laravel 4 that handles this?
UPDATE:
Here is my attempt to implement one of the answers. This is NOT working and I don't know what I am doing wrong.
application/routes.php
Route::controller('home');
Route::controller('Home_Controller', '/');
(you can see the edit history if you really want to look at some broken code)
And now domain.com/AboutYou and domain.com/aboutUs are returning 404. But the domain.com/home/AboutYou and domain.com/home/aboutUs are still returning as they should.
FINAL EDIT
I copied an idea from the PongoCMS routes.php (which is based on Laravel 3) and I see they used filters to get any URI segment and try to create a CMS page.
See my answer below using route filters. This new way doesn't require that I register every special route (good) but does give up redirects to the canonical (bad)
Put this in routes.php:
Route::controller('HomeController', '/');
This is telling you HomeController to route to the root of the website. Then, from your HomeController you can access any of the functions from there. Just make sure you prefix it with the correct verb. And keep in mind that laravel follows PSR-0 and PSR-1 standards, so methods are camelCased. So you'll have something like:
domain.com/aboutUs
In the HomeController:
<?php
class HomeController extends BaseController
{
public function getAboutUs()
{
return View::make('home.aboutus');
}
}
I used routes.php and filters to do it. I copied the idea from the nice looking PongoCMS
https://github.com/redbaron76/PongoCMS-Laravel-cms-bundle/blob/master/routes.php
application/routes.php
// automatically route all the items in the home controller
Route::controller('home');
// this is my last route, so it is a catch all. filter it
Route::get('(.*)', array('as' => 'layouts.locations', 'before' => 'checkWithHome', function() {}));
Route::filter('checkWithHome', function()
{
// if the view isn't a route already, then see if it is a view on the
// home controller. If not, then 404
$response = Controller::call('home#' . URI::segment(1));
if ( ! $response )
{
//didn't find it
return Response::error('404');
}
else
{
return $response;
}
});
They main problem I see is that the filter basically loads all the successful pages twice. I didn't see a method in the documentation that would detect if a page exists. I could probably write a library to do it.
Of course, with this final version, if I did find something I can just dump it on the page and stop processing the route. This way I only load all the resources once.
applicaiton/controllers/home.php
public function get_aboutUs()
{
$this->view_data['page_title'] = 'About Us';
$this->view_data['page_content'] = 'About Us';
$this->layout->nest('content', 'home.simplepage', $this->view_data);
}
public function get_featured_locations()
{
$this->view_data['page_title'] = 'Featured Locations';
$this->view_data['page_content'] = 'Featured properties shown here in a pretty row';
$this->layout->nest('content', 'home.simplepage', $this->view_data);
}
public function get_AboutYou()
{
//works when I return a view as use a layout
return View::make('home.index');
}
In my CI application I have a few controllers to handle different user-type functions:
CA
CD
DV
CSC
CB
So currently when someone logs in, he is redirected by his role to (example) : localhost/CA or localhost/CD etc..
I need to rewrite the routes to redirect everything depending on his role:
$route['(:any)'] = 'CA/$1'; (CA should not be hardcoded)
The rule should also be removed when using the login Controller (by filtering some url's)
Can anyone show me how to hook the rules after login? and also how to use a regexp to filter some url on which to apply the rules?
$route['^((?!auth/).)*$'] = '$1';
What other way would be to achieve this? .htaccess is out of the question since I need some data logic to create the routes.
Thanks to this wonderfull tutorial about routing from the database, I managed to add new routes after the user logs in (in my Auth controller):
class Auth extends CI_Controller {
function Auth() {
parent::__construct();
}
function index()
{
if ($this->ion_auth->logged_in())
{
$this->__checkRoles();
}
function __checkRoles()
{
$role=$this->ion_auth->get_group();
$this->save_routes($role);
redirect(base_url().'index');
}
public function save_routes($controller=null)
{
$output="<?php ";
//some uri's don't need routing
$output.="\$route['auth']='auth';";
$output.="\$route['auth/(:any)']='auth/$1';";
$output.="\$route['rest']='rest';";
$output.="\$route['rest/(:any)']='rest/$1';";
//for the rest route trough the "user-type" controller
$output.="\$route['(:any)']='".$controller."/$1';";
$this->load->helper('file');
//write to the cache file
write_file(APPPATH . "cache/routes.php", $output);
}
My routes.php looks like this:
$route['404_override'] = '';
$route['default_controller'] = "auth";
include_once(APPPATH."cache/routes.php");
It sounds like what you want is to implement remapping: the standard routing system is not designed to let you alter the routes based on whether or not the user is logged in.
However, you MIGHT (I've never seen this working) be able to put conditional statements in the standard routes.php file that checks to see if the user is logged in and what their role is (look at a session cookie or something like that) and then load different route options. Never tried it, but that MIGHT work for you.
I'm pretty new to code igniter.
Is there best practice for serving different view for different context.
For example, I'd like to serve specific pages for mobile user agents with the same controllers.
There isn't a hard rule for this. You can structure your view files however you like, and call $this->load->view() to load different view files for different outcomes in your controller. From my experience, CodeIgniter adapts very openly to how you organize your application's files.
In your example, perhaps I'd divide my system/application/views folder into two subfolders: main for desktop browsers, and mobile for mobile browsers:
system/
application/
views/
main/
index.php
some_page.php
...
mobile/
index.php
some_page.php
...
In an early part of your controller, say the constructor, you can decide what user agent is requesting it and then pick main or mobile based on that, then show your views accordingly from your controller actions.
Some quick code snippets to give you a better idea since you're new...
// Place this just below the controller class definition
var $view_type = 'main';
// Controller constructor
function MyController()
{
parent::Controller();
if ($this->agent->is_mobile())
{
$this->view_type = 'mobile';
}
else
{
$this->view_type = 'main';
}
}
// Example action
function some_page()
{
// ...
// This comes from the 'var $view_type;' line above
$this->load->view($this->view_type . '/some_page');
}
And some helpful references for you to explore:
Views in CodeIgniter
User Agent Class
Hope my explanation helps, and hope you have fun with CodeIgniter :)
Im new to symfony and have some simple questions. I am trying to understand the module system, but I dont understand how I create the actual homepage or other pages that are not based off of a model from the db. For example, the simple about page that has static info or the homepage that is a combination of a bunch of information from different models.
Can anyone help?
First of all, modules do not have to be restricted to a model from the database. You can have a Foo module which relies on no database content, and a Bar module that is primarily based on 3 different models. The module separation is a way to logically break up your site into manageable sections. Eg an e-commerce site might have a Products module, a Categories module and a Cart module and so on.
Your last sentence can then be split into 2 parts:
1) Static information can be on any page - if it's for things like "About us" and "FAQ" etc, I personally tend to use a "default" or "home" module, and create the various actions in there vis:
./symfony generate:module appname home
and
class homeActions extends sfActions
{
public function executeAbout(sfWebRequest $request)
{
// ...
}
public function executeFaq(sfWebRequest $request)
{
// ...
}
}
with the corresponding template files (aboutSuccess.php, faqSuccess.php).
2) A page can be comprised of data from many different models - just use your preferred ORM's method of retrieving data and set it to the view ($this->data = MyModel->findByColumn(...) etc). If you mean data from different modules, then you'd probably be better off looking at partials or components for elements of a page that can be used across different modules (navigation etc). See the Symfony docs for more details on these.
I'm used to handle static pages in this way.
First I create a new entry in apps/frontend/config/routing.yml:
page:
url: pages/:page
param: { module: page, action: index }
Then I write a "page" module (apps/frontend/modules/page/actions/actions.class.php):
<?php
class pageActions extends sfActions
{
public function executeIndex()
{
$this->page = $this->getRequestParameter("page");
$this->forward404Unless($this->_partialExists($this->page));
}
protected function _partialExists($name)
{
$directory = $this->getContext()->getModuleDirectory();
return (is_readable($directory.DIRECTORY_SEPARATOR."templates".
DIRECTORY_SEPARATOR."_".$name.".php"));
}
}
Last step, put in modules/page/templates/indexSuccess.php this code:
<?php include_partial($page); ?>
So all you have to do from now is to create a partial for each static page ie.
apps/frontend/modules/page/templates/_home.php which you can reach at
http://yousite/pages/home (without the need to add a new routing entry for every page)
You can create a module, e.g. called static and create actions for every static page or only one action that delivers the page depending on a request variable. The only thing this action does is loading a template.
IMHO it would be good if symfony comes with a default module for this.
For example actions of (my custom) module static:
class staticActions extends sfActions
{
public function executeIndex(sfWebRequest $request)
{
if(!$request->hasParameter('site')) {
return sfView::ERROR;
}
$this->site = $request->getParameter('site');
}
}
With this template:
//indexSuccess.php
<?php include_partial($site) ?>
The actual statics sites are all partials.
In my routing.yml looks like this:
# static stuff
about:
url: /about
param: {module: static, action: index, site: about}
This way you only have to create a new partial and a new routing entry when you add a static site and you don't have to touch the PHP code.
Another way to serve static pages without having to write any controller code is to set up the route something like the following:
myStaticPage:
pattern: /pageName
defaults:
_controller: FrameworkBundle:Template:template
template: MyBundle:Home:pageName.html.twig
Then just create your twig template and it should work fine.
Apart from the above, consider having a CMS for static pages, so you won't need technical savy people to mantain them or change them. This depends on the project, of course.
For really static and independent pages you can simply create any file in [pathToYourProjectRoot]/web directory.
It may by i.e. [pathToYourProjectRoot]/web/assets/static_html/about.html.
Then link to the page directly by http://your.site.com/assets/static_html/about.html.