Get name, locale etc in laravel - php

This may be very easy but I just can't find it.
When we use getLocale() we get value from config.app file, right?
In that file I have 'locale'=>'en'
Also there are other things like 'name', when I try to use app()->getName() or app()->name() it says it doesn't exist.
It works with config('app.name') same as config('app.locale'), I would like to know how does it work with locale but doesn't work for name?
Also I was searching for these methods getLocale but I can't find them that's main reason why am I asking this.

The function is declared at line 1039 in the Application class (Illuminate/Foundation/Application.php:1039) of Laravel 5.6.
That's why it's working for app()->getLocale() and not app()->getName().
/**
* Get the current application locale.
*
* #return string
*/
public function getLocale()
{
return $this['config']->get('app.locale');
}

Related

Symfony error - object not found

I am sorry for asking such a question and I know there have been questions like this before, but my case is rather...stupid. What I have is a project management system and I want to display all projects which works fine and is auto generated by Symfony. In the same controller which is ProjectController I made another action. I wanted to list all archived projects which have been completely done. For a start I simply copy pasted the code and the annotations and changed the routes and the name of the function. Here is my indexAction function
/**
* Lists all project entities.
*
* #Route("/", name="project_index")
* #Method("GET")
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$projects = $em->getRepository('AppBundle:Project')->findAll();
return $this->render('project/index.html.twig', array(
'projects' => $projects,
));
}
It is as simple as it can get. Now here is my archiveAction function which is the same
/**
* Lists all project entities.
*
* #Route("/archive", name="project_archive")
* #Method("GET")
*/
public function archiveAction()
{
$em = $this->getDoctrine()->getManager();
$projects = $em->getRepository('AppBundle:Project')->findAll();
return $this->render('project/index.html.twig', array(
'projects' => $projects,
));
}
So far I am not even filtering the projects, but doing the exact same thing as I do in the indexAction function and bear in mind both functions are in the same controller. Here is the error I receive when trying to show all archived projects -
AppBundle\Entity\Project object not found.
The index action works just fine and displays all projects, but if I change the route and change the name of the function and keep everything the same and they are in the same Controller - in one case it can find AppBUndle\Entity\Zadanie, but in the other case - it can't.
SOLVED I managed to solve the problem by putting the archive function on top. By on top I mean in first position before any other function. Before doing so I tried switching the routes. I took the route from indexAction and put it in the annotations from the archiveAction and the opposite. Both functions worked just fine. Then I returned them as they were and archive still didn't work. Then I simply put archive in first place before index and it worked as a charm. I have no idea what just happened and why but...didn't matter...it works. ( seriously though - if anybody knows why that is I would appreciate it)
EDIT: So 2 years and 4 months later I think I got the crank of it, the routes are cached so what I did was just clear the cache and it worked, but at the time I didnt pay attention to this detail
I found the same problem and I managed to find the cause of the problem.
routing problem, for example:
#Route ("/ {id}", name = "project_archive_show")
function2
#Route ("/ archive", name = "project_archive")
function1
urls must have personalized specifications:
for example :
#Route ("/ show / {id}", name = "project_archive_show")
function2
or says that id is an integer

PHP call_user_func a namespace alias

I'm wondering if it's possible to call a function from a name spaced file.
Currently the code below produces an error. I know it's possible to just pass the whole namespace, but is there a more automated way just using the alias of U?
use Helpers\User as U;
$t = call_user_func('U\something', 'test');
var_dump($t);
UPDATE
The way I solved this was by creating a function within that name spaced file that would return the file name spaced.
/**
*
* pntfn = Prepend namespace to function name
*
* #param $fn
*
* #return string
*/
function _pntfn($fn) {
return __NAMESPACE__.'\\'.$fn;
}
This then let's me do the following in a file
use Helpers\Users as U;
If I wanted to compose (functional programming) a function from my Users namespace file I would do the following;
$get_username_upper = F\compose('strtoupper', U\_pntfn( 'get_username' ));
Not my favorite implementation, but works for now. The con, is that you'd have to add this to all of your name spaced function files.
The trouble is U is resolved at compile time, so calling:
U\something
Literally becomes:
Helpers\User\something
This might still be invalid, since I know we also have the directive use function, so you may want to check that out as well.
Anyway, the string 'U\something' is a runtime constant, meaning U at this point doesn't actually refer to anything at all. So no, unfortunately this isn't possible.

How to create a route with custom path in Symfony RoutingBundle (PHPCR)?

I'm currently researching Symfony CMF and PHPCR for a project I recently started. What I'm currently trying to figure out is how to create a Route and save it into the database. As far as I understand, I must use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route and persist the element into the database. This works fine, but automatically generates a route path, which is not what I want. What I need to do is generate a custom route which links to a specific controller. Here is my code:
$em = $this->get('doctrine_phpcr.odm.document_manager');
$parent = $em->find(null, '/cms/routes');
$route = new \Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route();
$route->setParentDocument($parent);
$route->setName('my_route_name');
$route->setDefault('_controller', 'AppBaseBundle:Frontend/Users:index');
$em->persist($route);
$em->flush();
If i execute this code, the generated route will be /cms/routes/my_route_name. From what I can see, you could use $route->setPath('/testing');, but that generates the following exception:
Can not determine the prefix. Either this is a new, unpersisted document or the listener that calls setPrefix is not set up correctly.
Does anybody have any ideas how to solve this?
In PHPCR, every document has a path where it is store. If you are familiar with doctrine ORM, the path has the role of the ID. The difference with ORM is that all documents (regardless of their type) live in the same tree. This is great, because your route can reference just anything, it is not limited to specific document types. But we need to create some structure with the paths. This is why we have the prefix concept. All routes are placed under a prefix (/cms/routes by default). That part of the document path is removed for the URL path. So repository path /cms/route/testing is the url domain.com/testing.
About your sample code: Usually, you want to configure the controller either by class of the content document or by route "type" attribute to avoid storing a controller name into your database to allow for future refactoring. A lot of this is explained in the [routing chapter of the CMF documentation][1] but the prefix is only used there, not explicitly explained. We need to improve the documentation there.
[1] http://symfony.com/doc/master/cmf/book/routing.html
I managed to find a way to overcome this issue. Because in my project I also have the RouteAutoBundle, I created a class which extends \Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route. Inside that class I added:
/**
* #PHPCR\Document(referenceable=true)
*/
class MenuRoute extends Route
{
protected $url;
/**
* Get $this->url
*
* #return mixed
*/
public function getUrl() {
return $this->url;
}
/**
* Set $this->url
*
* #param mixed $url
*/
public function setUrl($url) {
$this->url = $url;
}
}
After that I added this to cmf_routing_auto.yml:
App\MenuBundle\Document\MenuRoute:
uri_schema: /{getUrl}
token_providers:
getUrl: [content_method, { method: getUrl }]
So now one would just create an instance of MenuRoute (just like when using Route) and call the method setUrl($your_url) passing the desired url.
If anybody finds a better way, I'm opened to suggestions.

Get default route from dependency injection services

I am using request object in twig extension class to get the current route. For instance, having the following url:
http://www.localhost/project/user/page/2
Inside of the twig extension I'm able to get user/page/2 string and do something with it.
The problem arises when I wanna get the default route using the same method, which I have to do. For example, accessing the following url:
http://www.localhost/project/user
I want to get user/page/1 string inside the twig extension class, and not just user.
The controller looks like this:
/**
* #Route(name="user",
* default="user/page/1")
*/
Is there a way to achieve that? Or do I have to stop using default routes?
Write a comment if you need more explanation, it's 9AM here in Poland and I'm sleeping yet.
The #Route documentation explains that you can do this for set a default page number:
/**
* #Route("/project/user/page/{page}",
* name="user",
* defaults={"page" = 1},
* requirements={"page" = "\d+"}
* )
*/

Symfony 2 URL Parameters?

I am used to Yii's URL management:
site.com/controller/action/var1/value1/var2/value2
That way I can put my variables in any order in the URL or omit one and having a default value asigned:
public function actionFoo( $var1=22, $var2 ) { //Var1 is optional, Var2 must come.
}
Is there such thing in Symfony? I've searched but I found only hard-coded URL positions, like CodeIgniter, and that's something I find very annoying, because if I have a parameter in the middle of the URL I need to necessarily give it a value.
Example:
site.com/controller/action/false/false/value3
Maybe what I am used too is a bad practice, and I am open to learn other way.
Thanks
Symfony doesn't work that way.
If you need to map a value to a variable name then you need to use query strings: ?var1=value1&var2=value2.
Then, in your controller you can do $this->get('request')->query->get('var1', false) (false being the default value if var1 isn't set).
Otherwise you can define a default value in your route but it doesn't work the way you expect it to work.
You could extend the Routing component to mimic Yii's routing system but I wouldn't advise it because it would require some time.
All-in-all query strings will do exactly what you expect.
I have also found it to be a bit annoying as well, but with a little bit of work you can make the URLs work in Symfony by setting multiple routes to the same controller method. The catch is the order does matter.
This example use annotation but you could do the same thing with YAML, XML and PHP.
/**
* #Route("/whatever/{var2}/{var1}", name="whatever_allvars")
* #Route("/whatever/{var2}", name="whatever_onevar")
* #Template()
*/
public function actionFoo( $var1=22, $var2 ) {
//Var1 is optional, Var2 must come.
}
this would allow the following URLs:
site.com/whatever/var2/var1
site.com/whatever/var2
if you really want to make it match the sample URL give you could set it up like this.
/**
* #Route("/controller/action/var1/{var1}/var2/{var2}", name="whatever_allvars")
* #Route("/controller/action/var1/{var1}", name="whatever_onevar")
* #Template()
*/
public function actionFoo( $var1, $var2=22 ) {
//Var2 is optional, Var1 must come.
}
This would allow the following URLs:
site.com/controller/action/var1/value1/var2/value2
site.com/controller/action/var1/value1
Hopefully this gives you an idea what can be done in Symfony.

Categories