How to add own namespace in Yii2 - yii2

I have changed Yii2 advanced directory structure like following(it's working well):
app-folder
-admin
-assets
-.htaccess
-index.php
-assets
-protected
-backend
...
-common
...
-frontend
...
...
-uploads
...
Now, I am trying to add a namespace as namespace protected\base; into protected/base/AnyFile.php file and use it in a controller as use protected\base\AnyFile;. But, my project is giving error:
syntax error, unexpected 'protected' (T_PROTECTED), expecting identifier (T_STRING) or function (T_FUNCTION) or const (T_CONST) or \\ (T_NS_SEPARATOR)
I saw this issue on the website: Yii2 Custom / Shorter Namespace. However, It didn't work on my condition.

First of all protected is reserved keyword (token T_PROTECTED). You can keep direcory name but you need to change namespace root alias.
In your alias config file protected/common/bootstrap.php write:
Yii::setAlias('app', dirname(dirname(__DIR__))); // set path to protected directory
And then use namespace app\base; and use app\base\AnyFile;.
See Class Autoloading section of the guide https://www.yiiframework.com/doc/guide/2.0/en/concept-autoloading

Related

no ReserveMatch found for seemingly correct path converter

I have the following anchor in my HTML template
{{col.1}}
and the following line within urls.py
path('experiments/<str:table_name>/<uuid:foreign_key>/<str:foreign_attribute>/str:foreign_table_name>/', ExpViews.show_foreign_key, name='experiments_tables_foreign_key')
show_foreign_key exists in views.py and is a function that I'd like to use for a view. It accepts 4 parameters.
I have the correct namespace done aswell. I get the following error:
Reverse for 'experiments_tables_foreign_key' with arguments '('test_2', UUID('7a4c1cb5-6a7c-4fd3-8eea-8e9bef41802d'), 'ID', 'test_1')' not found
Here is a typo on the url.
path('experiments/<str:table_name>/<uuid:foreign_key>/<str:foreign_attribute>/str:foreign_table_name>/', ExpViews.show_foreign_key, name='experiments_tables_foreign_key')
^^^ # lacks a <
So, change this to:
path('experiments/<str:table_name>/<uuid:foreign_key>/<str:foreign_attribute>/<str:foreign_table_name>/', ExpViews.show_foreign_key, name='experiments_tables_foreign_key')

Get directory path in PhpStorm Live Template

I'm trying to generate a namespace through PhpStorm's templating system (File and Code Templates), I can't see any helpers / means of getting this through PhpStorm's documentation.
I was wondering if there was a way to automatically define the namespace based on the directory:
<?php
// I'm wanting to apply to $namespace
namespace $namespace;
class ${NAME} implements
\Zend\Mail\Mailable
{
/** \Zend\Mail\Factory */
private $factory;
...
}#set( $factory = "factory" ) #set( $this- = "this-" ) #set( $directory = ?)
If my directory/filename is zend/mail/model/notice/send.php I'd like this to generate a namespace of Zend\Mail\Model\Notice; I can get the class name through the ${NAME} variable.
I'm aware I can have the template request the namespace through a dialog when the file is being created, however I was wondering if it's possible to automate this?
if you want , you can use groovyScript.
for example, you can create a variable $FOLDER_OF_FILE$ and set this groovyScript to take the folder of file.
groovyScript("String[] p=_editor.getVirtualFile().getPath().split('/');String prevLast = p[p.length-2];")
you can change and adapt my example for your needs
There are no predefined variables for file path, please vote for IDEA-136387 to be notified on any progress with this feature.

How to create new phpDoc annotations in PhpStorm 8

I searched via Google but couldn't find an answer to this. PhpStorm has many built in annotations for code completion but many are missing as well. I am pretty sure there is a way to create new annotations but couldn't find it anywhere within the settings. Or maybe it is an XML file somewhere. NetBeans has support for this feature.
In other words, how can I create new phpDoc annotations for completion in phpStorm 8 like #usesDefaultClass for phpUnit.
I have found this answer here: http://blog.jetbrains.com/webide/2013/02/phpdoc_and_code_templates/
Hope this will help you
PhpDoc Templates
PhpDoc templates are located under Settings | File Templates | Includes. Currently there are three templates named PHP Class Doc Comment, PHP Function Doc Comment and PHP Field Doc Comment. Any of these can be included into other templates via the #parse directive. For example, we can modify the original class template (Templates | PHP Class) to also include the class PHP Doc when a class is generated:
<?php
#parse("PHP File Header.php")
...
#parse("PHP Class Doc Comment")
class ${NAME} {
}
The same templates are used when:
A new PHP Doc comment is generated after we type /** and press Enter before a class, function (method) or class field.
We invoke Code | Generate | PHPDoc blocks or use the Add PHP Doc quick-fix for the inspection Missing PHP Doc.
Below is a list of variables that can be used in PHP Doc templates:
${NAME}
The element (class, function, field) name.
${NAMESPACE}
The name of the namespace the element belongs to without any leading or trailing backslashes, for example Core\Widgets. The variable value is empty if the element doesn’t belong to any namespace. A check like `#if (${NAMESPACE})` ... is possible.
${CLASS_NAME}
Contains a class name for class methods and fields. Will be empty for functions that do not belong to any class.
${TYPE_HINT}
For functions (methods), contains the return type of the function (method). For fields, evaluates to the field’s type if it can be found, for example, from a default value. Will be empty if the type cannot be retrieved from the code.
${STATIC}
Takes the value of “static” if the function or field is static, otherwise, an empty string. We can use this variable with the condition `#if (${STATIC})` ... to generate something specific for static class members.
${CARET}
Marks the position where the editor caret should be placed after the comment is added. Note: Works only if the comment is added after we type “/**” and hit Enter. Should be placed inside the comment, not on the first line /** or on the last line */. In all other cases the caret marker will be ignored.
${PARAM_DOC}
A generated PHP Doc fragment containing function (method) parameters in the form: “* #param type $name“. For example, if the function signature is foo ($x, $y), it will evaluate to:
#param $x
#param $y
${THROWS_DOC}
A generated PHP Doc fragment containing exceptions thrown from function (method) body in the form * #throws ExceptionName. One exception per line/#throws tag. For example:
#throws DOMException
#throws HttpException
Code Templates for Overridden/Implemented Methods
The following templates can be found under Settings | File Templates | Code: PHP Implemented Method Body and PHP Overridden Method Body. There are few parameters, considering that in most cases we will need either a simple call to a parent method or just our own comment (some version of TODO perhaps):
${NAME}
Method name.
${PARAM_LIST}
A comma-separated list of parameters. For example, if the original method signature is foo(Bar $bar, $y), the variable will take the value “$bar, $x” which can be used in a call to the parent method as `${NAME}(${PARAM_LIST})`”
${RETURN}
Either “return” or an empty string.
A Dollar Sign Variable: ${DS}
Solves the problem of putting a dollar sign $ anywhere within the template. Actually, the dollar sign is used both in PHP and in Velocity template engine taking care of code generation behind the scenes. So whenever we need a dollar sign, just use ${DS} as its equivalent. For example, if we want $this->foo() to be generated, we need to put ${DS}this->foo(). This may not look perfect but guarantees that there will be no conflicts.

How to access to a function of a controller from inside another controller in Symfony2?

I would like to know how to access to a function of a controller from inside another controller in Symfony2. In fact I have two controllers: "EventgroupeController" and "GroupeController". In the code of the controller "EventgroupeController" I put the instruction below:
return GroupeController::AfficheGroupeAction();
But when I run the code (or let's say the project I am developing), it displays this error message in Symfony2:
ContextErrorException: Runtime Notice: Non-static method Ikproj\GroupeBundle\Controller\GroupeController::AfficheGroupeAction() should not be called statically, assuming $this from incompatible context in C:\wamp\www\Wkayet_project\PFESymfony2\src\Ikproj\GroupeBundle\Controller\EventgroupeController.php line 104
After having a look at this link: How to access a different controller from inside a controller Symfony2 in order to know how to access a different controller from inside a controller in Symfony2, I modified the content of the file services.yml as below:
parameters:
# ikproj_groupe.example.class: Ikproj\GroupeBundle\Example
services:
# ikproj_groupe.example:
# class: %ikproj_groupe.example.class%
# arguments: [#service_id, "plain_value", %parameter%]
controllerservice:
class: Ikproj\GroupeBundle\Controller\GroupeController
Then, I replaced the instruction: return GroupeController::AfficheGroupeAction(); by the lines below:
$yourController = $this->get('controllerservice');
$yourController1 = $yourController::AfficheGroupeAction();
return $yourController1;
But I still see this error message:
ContextErrorException: Runtime Notice: Non-static method Ikproj\GroupeBundle\Controller\GroupeController::AfficheGroupeAction() should not be called statically, assuming $this from incompatible context in C:\wamp\www\Wkayet_project\PFESymfony2\src\Ikproj\GroupeBundle\Controller\EventgroupeController.php line 106
So, my question is: how can I resolve this problem and how can I access to the function AfficheGroupeAction() of the controller "GroupeController" from inside the controller "EventgroupeController"?
An action method must not be static.
$this->get('controllerservice')->youMethod();
Should work !
But with a good "application design", you should not have this need except if you want to forward a request from a controller to another ( example : backward compatibility ). In this case you can use the forward method provided by symfony2 base controller. ( http://symfony.com/doc/current/book/controller.html )

How do I define custom function to be called from IPython's prompts?

I had an old ipy_user_conf.py in which I included a simple function into the user namespace like this:
import IPython.ipapi
ip = IPython.ipapi.get()
def myfunc():
...
ip.user_ns['myfunc'] = myfunc
Then, I could use myfunc in the prompt.
However, I updated to IPython 0.12.1 and now the ip_user_conf.py does not work. I haven't seen how to translate such a custom function for prompts to the new configuration model.
Which is the way to do this?
Best regards,
Manuel.
UPDATE: Changed the subject to question
After reading a bit of the documentation (and peeking at the source code for leads) I found the solution for this problem.
Simply now you should move all your custom functions to a module inside your .ipython directory. Since what I was doing was a simple function that returns the git branch and status for the current directory, I created a file called gitprompt.py and then I included the filename in the exec_file configuration option:
c.InteractiveShellApp.exec_files = [b'gitprompt.py']
All definitions in such files are placed into the user namespace. So now I can use it inside my prompt:
# Input prompt. '\#' will be transformed to the prompt number
c.PromptManager.in_template = br'{color.Green}\# {color.LightBlue}~\u{color.Green}:\w{color.LightBlue} {git_branch_and_st} \$\n>>> '
# Continuation prompt.
c.PromptManager.in2_template = br'... '
Notice that in order for the function to behave as such (i.e called each time the prompt is printed) you need to use the IPython.core.prompts.LazyEvaluation class. You may use it as a decorator for your function. The gitprompt.py has being placed in the public domain as the gist: https://gist.github.com/2719419