How to use Router Connect method correctly in CakePHP 2 and 3? - redirect

I have a controller MyUsersController with an action login.
I have mapped this with:
$routes->connect('/member/login', [ 'controller' => 'MyUsers', 'action' => 'login', 'plugin'=>false, 'prefix'=>FALSE]);
Now I can access this action "login" using these two urls which is not good for SEO.
http://localhost/cakephpapp/member/login
http://localhost/cakephpapp/my-users/login
So, how can I disable the second URL?
I have tried this code which is working fine but I don't know whether this is correct method or not.
$routes->redirect('/my-users/login', '/member/login');

There is a line in the default routes file that provides the fallbacks so that you can access pages using controller/action. This is really to help you quickly scaffold an app. In CakePHP 3 this is:-
$routes->fallbacks('InflectedRoute');
As commented in the file you can/should remove this line once you have set up the routes for your application. If you remove this it will also prevent your error logs from filling with errors like 'Missing Controller' which can be an issue on a production site, particularly if it is replacing an existing one.
In CakePHP 2 the equivalent line is this:-
require CAKE . 'Config' . DS . 'routes.php';
Removing these lines does mean that you will have to define all routes for your application, but that is probably a better practice than relying on automagic routes of Cake (or any framework). There's a good article on this by Phil Sturgeon: Beware the Route to Evil.

Related

Selenium, Web Scraping, can't access the class

I am very new to web scraping, It's been several days that I am dealing with the same problem:
Please look at the below line of code(extracted directly from the web page):
< option value='pick' id='ember2314' class='x-option ember view'>To Pick</option
whatever I do I can't access that class:
driver.find_element_by_class_name('x-option ember view') #when I want to print the text here, it says unable to locate element.
But for some other cases, I can easily access the class, and sometimes for some cases, I can't access the class.
Can anyone please shed some light on this? (sorry, I am very new to web scraping)
Please note that the 'id' and 'value' are changing every time so I can't rely on them.
Any help would be much appreciated.
Thanks,
For the people who are beginners like me, here is the solution. It is easy to search it with it's xpath:
//tagename[#attribute='value of the attribute']
so for this case:
driver.find_element_by_xpath('//option[#class="x-option ember view"]')
would do the trick.
From my understanding, the 'class' here is actually an attribute of the tag 'option', so search it like this: find_element_by _class_name('x-option ember view') won't give you anything.

Include a module with a ZendFramework library

I'd like to setup a profiler for my company's ZendFramework and Doctrine installation. Right now I have a bar at the bottom that shows up when on a dev environment that gives some basic timing and query counts (much like the Symfony profiler bar). What I'd like to do, is store all of that information and more in a SQLite database and allow viewing of that information in a profiler like Symfony allows.
Is there a way that allows me to include a module with my company's library where all of the code for this profiler can sit? Ideally, I'd want it setup so that a developer could type in "domainname.com/CompanyProfiler" and it would show them the full screen profiler. It doesn't seem like there currently is a way for me to make that routing possible without a new module.
Edit: After seeing the answer about setControllerDirectory, I looked into the front controller methods and found addModuleDirectory, which sounds like exactly what I need. But I can't get it to work.
$frontController->addModuleDirectory(APPLICATION_PATH . '/../library/Company/modules');
If I do a getControllerDirectory after that, I see:
'profiler' => '{really long correct path}../library/Company/modules\profiler\controllers'
In the "controllers" folder, I have IndexController.php with a class name of:
class Profiler_IndexController extends Zend_Controller_Action
But if I try to go to the URL "/profiler", I get a controller not found error. Any thoughts on what I'm doing wrong?
If I var_dump the errors in my error controller, I can see that it is clearly trying to access the default module.
If I understood right, you also want to have the controllers in the library? If so, you can set that ZF looks for CompanyProfiler module where all the controllers are in the library (or anywhere else) using setControllerDirectory().
Try adding a direct route to the module by placing the following in your bootstrap:
protected function _initRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
// Route for profiler
$route = new Zend_Controller_Router_Route(
'profiler/:controller/:action/*',
array(
'module' => 'profiler',
'controller' => 'index',
'action' => 'index'
)
);
$router->addRoute('profiler', $route);
}

configuration in Zend Framework

i am new to ZF and i am making a project in ZF .i got basic knowledge of ZF but need more.
i studied configuration in ZF in many different ways like
Using Array Configuration
Creating File Configuration
Using INI File Configuration
Using XML File Configuration
i need to know what is best and efficient way among all these in sense of Maintainability and security ??
also can anyon plz tell me (Quick advise) how to start my first project so that its easy for me in order to maintain and upgrade.i am bit confuse with layouts that is header , footer and sidebar etc
how to intigrate this in my bootstrap like this but its not working
// Register the autoloader
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
// Initialise Zend_Layout's MVC helpers
Zend_Layout::startMvc(array('layoutPath' => ROOT_DIR.'/app/views/layouts'));
For configuration the answer is: use what you like best. All Zend_Config classes have same functionality. They just differ with the syntax.
As for something not working you must write more details. Again - what do you mean by 'not working'? What are the errors?

How to get request properties in routeStartup plugin method?

I'm developing a multilanguage application, and use routes with translated segments. For multilingual support I created special Multilingual plugin.
To use translated segments I need set translator for Zend_Controller_Router_Route before routes init. So only possible place for this in my plugin is routeStartup method, but there is one problem here - for determine right locale I need to use properties of request (Zend_Controller_Request_Abstract), like module, controller and action names, but they are not defined yet here in routeStartup method. They are already defined, for example, in routeShutdown - but I can't set translator for route there, because it have to be done before routes init.
So what can I do:
can I get request properties somehow in routeStartup
or can I re-setup translator later in routeShutdown
P.S: there is a question with exactly the same problem Zend_Controller_Router_Route: Could not find a translator, but proposed answers is not the option for me, because I can't just retrieve language code from url with Regex, I have much more complicated code to define right language code.
Thanks.
What about putting your code in preDispatch? That's what I personally do when I need to check if the person is logged in. Maybe you can move your code there too?

symfony/zend integration - blank screen

I need to use ZendAMF on a symfony project and I'm currently working on integrating the two.
I have a frontend app with two modules, one of which is 'gateway' - the AMF gateway. In my frontend app config, I have the following in the configure function:
// load symfony autoloading first
parent::initialize();
// Integrate Zend Framework
require_once('[MY PATH TO ZEND]\Loader.php');
spl_autoload_register(array('Zend_Loader', 'autoload'));
The executeIndex function my the gateway actions.class.php looks like this
// No Layout
$this->setLayout(false);
// Set MIME Type
$this->getResponse()->setContentType('application/x-amf; charset='.sfConfig::get('sf_charset'));
// Disable cause this is a non-html page
sfConfig::set('sf_web_debug', false);
// Create AMF Server
$server = new Zend_Amf_Server();
$server->setClass('MYCLASS');
echo $server->handle();
return sfView::NONE;
Now when I try to visit the url for the gateway module, or even the other module which was working perfectly fine until this attempt, I only see a blank screen, with not even the symfony dev bar loaded. Oddly enough, my symfony logs are not being updated as well, which suggests that Synfony is not even being 'reached'.
So presumably the error has something to do with Zend, but I have no idea how to figure out what the error could be. One thing I do know for sure is that this is not a file path error, because if I change the path in the following line (a part of frontendConfiguration as shown above), I get a Zend_Amf_Server not found error. So the path must be correct. Also if I comment out this very same line, the second module resumes to normality, and my gateway broadcasts a blank x-amf stream.
spl_autoload_register(array('Zend_Loader', 'autoload'));
Does anyone have any tips on how I could attach this problem?
Thanks
P.S. I'm currently running an older version of Zend, which is why I am using Zend_Loader instead of Zend_autoLoader (I think). But I've tried switching to the new lib, but the error still remains. So it's not a version problem as well.
got it...
I was not using
set_include_path()
while loading Zend. It's still odd that it would give such a cryptic error, but this was the missing piece indeed.