Within EasyAdmin for Symfony you can use the AdminUrlGenerator for easily generating URLs to for example EasyAdmin CRUD Controllers.
Documentation here: https://symfony.com/doc/current/EasyAdminBundle/crud.html#generating-admin-urls
In my case i want to generate an URL to CRUD controller which is also linked within the Dashboard. If i create a link to a CRUD controller, the link works, but the corresponding MenuItem is not highlighted.
I found out, that EasyAdmin highlights the MenuItem with an URL parameter calling menuIndex. I can unset the menuIndex during Link Generation, but then no menuItem is highlightd in the menu.
I havn't found any information on how to get the correct menuIndex for a generated CRUD URL.
So how can i generate Admin URLs with correct menuIndex?
I want to give one possible answer. But to be honest, i don't really like my solution. Maybe someone has a better solution.
So, i am iterating over the configured menuItems within the DashboardController and trying to determine the menuIndex for a given Entity.
The code lookes like this:
private function getIndexLinkForCrudController(string $controller): string
{
return $this->adminUrlGenerator
->unsetAll()
->setController($controller)
->setAction(Action::INDEX)
->set('menuIndex', $this->determineMenuIndexForEntity($controller::getEntityFqcn()))
->generateUrl();
}
private function determineMenuIndexForEntity(string $entity): ?int
{
$menuItems = $this->configureMenuItems();
foreach ($menuItems as $id => $menuItem) {
/* #var MenuItemInterface $menuItem */
$routeParameter = $menuItem->getAsDto()->getRouteParameters();
if (
is_array($routeParameter) &&
array_key_exists(EA::ENTITY_FQCN, $routeParameter) &&
$routeParameter[EA::ENTITY_FQCN] == $entity
) {
return $id;
}
}
return null;
}
This code works for me. But this code only works within the DashboardController. If i want to create Admin URLs within a CRUD controller i need to move the menu config to a static method and accessing it there.
Also i am not fetching the error case when i cannot determine the menuIndex and returning null. For my case it's fine.
Maybe this is helpfull for someone.
If somebody has better solution, i would be happy to here about it.
Related
I'm using Silverstripe FullTextSearch. My interrogation is how search in a custom field and show results. There is code write in index.md :
File: mysite/code/MyIndex.php:
<?php
class MyIndex extends SolrIndex {
function init() {
$this->addClass('MyPage');
$this->addFulltextField('Description');
}
}
In Page.php
class Page_Controller extends ContentController {
private static $allowed_actions = array('search');
public function search($request) {
$query = new SearchQuery();
$query->search($request->getVar('q'));
return $this->renderWith('array(
'SearchResult' => singleton('MyIndex')->search($query)
));
}
}
When I'm trying to search some words from description field, no results are found... Suggestions?
In my experience FullTextSearch has been frustrating and problematic. I am willing to be corrected, but I feel like the devs are moving away from it in place of using SearchFilters (https://docs.silverstripe.org/en/3.1/developer_guides/model/searchfilters/).
If it is helpful at all I wrote a module that allows for custom searching and displaying the results in a custom controller. It isn't as efficient as FullTextSearch but works pretty well (https://github.com/i-lateral/silverstripe-searchable).
I did originally try to write this module using FullTextSearch, but I just couldn't get it working.
Well, I have found a solution. FullTextsearch module always search in $Title, $MenuTitle and $Content. Instead of using $Content for my content, I use $ContentPage variable. All DataObjects and other variables must must added like bellow.
Example :
$this->Content = $this->ContentPage." ".$this->Variable1." ".$this->Variable2." ".$dataobject;
In the example, all variables and dataobject in the page is searchable throught $Content. That's not a perfect solution, but works.
I'm using codeigniter multilanguage and it works fine. The problem is when I try to do multilanguage in the URL... how can I do it?
I mean the controller has to be a file, with a name, and its functions too... so I can't figure how can I do it.
The only alternative I thought is create the same controllers for each language I need... but this is a lot of repeated code just for change the name of the controller and functions... and the maintenance will be a big trouble.
Any help?
Pass the language indicator as a "GET" value to your controller functions:
eg.
base_url/controller/inventory/en
Then use it like this in your controller:
/**
* #desc This will get called when no method specified
* Will show home page (list of items)
*/
function inventory($lang="en",$from=0){
// load proper language file
$this->lang->load('language_filename', $lang);
// generate db where clause
$where = array(
"published"=>"1",
"language"=>$lang
);
// paging
$this->_setPagingLinks($this->newsModel->getTotalRecordsNumber($where),10,4,"inventory/".$lang,$lang);
// loading items from db
$this->data["news"] = $this->newsModel->getRecords($where,$from,10,"time");
// load the view according to language
$this->data["content"] = $this->load->view("$lang/news",$this->data,TRUE);
$this->load->view("$lang/container",$this->data);
}
I am looking for solution to show the url with username. I am using Joomla 3.3.0 stable version.
Ex. site_url/userp-username
I tried to solve this using .htaccess with following rules that I have used for my core PHP websites.
RewriteRule ^userp-([a-zA-Z0-9-_]+)/?$ site_url/index.php?option ... er_name=$1 [R=301,L]
When I hit the url for example http://sitename.com/userp-vishal07 it execute the code that I want to call for this url and it shows the results correctly. But url does not remain as it is and turn into http://vicciivital.com/index.php/en/component/users/profile?layout=view_profile&user_name=vishal07
I am not able to understand how the Joomla redirect works. Please correct me if I am doing any mistakes here.
For reasons I have never understood the com_users router does not route any profiles except the users own profile.
/**
* Method to get a route configuration for the profile view.
*
* #return mixed Integer menu id on success, null on failure.
* #since 1.6
*/
public static function getProfileRoute()
{
// Get the items.
$items = self::getItems();
$itemid = null;
// Search for a suitable menu id.
//Menu link can only go to users own profile.
foreach ($items as $item)
{
if (isset($item->query['view']) && $item->query['view'] === 'profile')
{
$itemid = $item->id;
break;
}
}
return $itemid;
}
What you would need to do is extend this method to handle everyone's profiles. Just make sure to deal with the situation that there is a content item or tag with the same alias as the alias for the user.
In general the easiest workaround is to use com_contact as a profile instead. Turning on the contact creator plugin will create contacts for your new users automatically and contact can display anything from a profile plugin. Also it can display articles by the user and then you can also add plugins for other things if you want. To me it always works a lot better then messing with the com_users profile.
I'm writing a control panel for my image site. I have a controller called category which looks like this:
class category extends ci_controller
{
function index(){}// the default and when it called it returns all categories
function edit(){}
function delete(){}
function get_posts($id)//to get all the posts associated with submitted category name
{
}
}
What I need is when I call http://mysite/category/category_name I get all the posts without having to call the get_posts() method having to call it from the url.
I want to do it without using the .haccess file or route.
Is there a way to create a method on the fly in CodeIgniter?
function index(){
$category = $this->uri->segment(2);
if($category)
{
get_posts($category); // you need to get id in there or before.
}
// handle view stuff here
}
The way I read your request is that you want index to handle everything based on whether or not there is a category in a uri segment. You COULD do it that way but really, why would you?
It is illogical to insist on NOT using a normal feature of a framework without explaining exactly why you don't want to. If you have access to this controller, you have access to routes. So why don't you want to use them?
EDIT
$route['category/:any'] = "category/get_posts";
That WOULD send edit and delete to get_posts, but you could also just define those above the category route
$route['category/edit/:num'] = "category/edit";
$route['category/delete/:num'] = "category/delete";
$route['category/:any'] = "category/get_posts";
That would resolve for the edit and delete before the category fetch. Since you only have 2 methods that conflict then this shouldn't really be that much of a concern.
To create method on the fly yii is the best among PHP framework.Quite simple and powerful with Gii & CRUD
http://www.yiiframework.com/doc/guide/1.1/en/quickstart.first-app
But I am a big CI fan not Yii. yii is also cool though.
but Codeigniter has an alternative , web solution.
http://formigniter.org/ here.
I'm looking for a way to programatically get a list of controllers in a Kohana application.
Something like:
public function build_site_map(){
$controllers = Kohana::get_controllers();
echo '<ul>';
foreach($controllers as $controller){
echo '<li>'.$controller.'</li>';
}
echo '</ul>';
}
I realize I could read the /application/classes/controllers/ directory, but I'm hoping there's an easier way.
Thanks,
Getting a list of your controller files could be done with Kohana::list_files('classes/controller'). But as Michal already said, there isn't a 1:1 realtionships between controllers/actions and routes.
I'm afraid there is no Kohana::get_controllers() method that you can easily call to get a a sitemap of sorts. This is because controllers are called dynamically, i.e. based on the request's URL and Routes configuration Kohana's checking whether a controller (and action) exist and then call them. Kohana does not keep record of all available controllers and actions that can be accessible.
Neither traversing the /application/classes/controllers directory and getting the list of all files would give you the desired result, because there are not only actions to be read (which can be fairly easily done with Reflection class), but there are also Routes which you have to take into account.
As you can see this is potentially very complex issue and one that cannot be simply answered with a snippet of code that can be pasted here.
If you decide to write a script that would actually create such a map, but you stumble into a problem on the way, we would be able to more helpful then otherwise this question is too open. Also, if you were to write it, I suggest you create it as a module that you would be able to include in any other projects and share it.
Here my solution to get all the controllers and their actions. I use it to add permissions into our system https://github.com/open-classifieds/openclassifieds2/
/**
* get all the controllers and the actions that can be used
* #return array
*/
public static function list_controllers()
{
$list_controllers = array();
$controllers = Kohana::list_files('classes/controller');
foreach ($controllers as $controller)
{
$controller = basename($controller,'.php');
$list_controllers[] = $controller;
$class = new ReflectionClass('Controller_Panel_'.$controller);
$methods = $class->getMethods();
foreach ($methods as $obj => $val)
{
if (strpos( $val->name , 'action_') !== FALSE )
{
$list_controllers[$controller][] = str_replace('action_', '', $val->name);
}
}
}
return $list_controllers;
}