laravel route and controller not working - laravel-5.1

Laravel 5.1
//////////////////////////////////////////////////////
My Route://
Route::resource('/books', 'BookController#index');
////////////////////////////////////////////////////////
My BookController//
<?php
namespace App\Http\Controller;
use App\Book;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class BookController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$books=Book::all();
return view('books.index',compact('books'));
}
}
/////////////////////////////////////////////////////////
My Url:
http://localhost:8000/books
/////////////////////////////////////////////////////////
My Browser show this error//
Whoops, looks like something went wrong.
1/1 ReflectionException in
C:\xampp\htdocs\bookstore\vendor\laravel\framework\src\Illuminate\Container\Container.php
line 737:
Class App\Http\Controllers\BookController does not exist

Notice the error says Class App\Http\Controllers\BookController does not exist. That doesn't match your namespace set in the BookController class.
It's looking for your class in the "App\Http\Controllers" location with an "s". Your namespace says "App\Http\Controller" (without the "s"). Fix your namespace and it should work.

You are using a resourceful route and you don't need to specify the method.Also the / before the books is not needed.
Your route should be like this:
Route::resource('books', 'BookController');

Related

Class not found for custom plugin block when trying to do dependency injection

I'm attempting to create a custom plugin block in Drupal. When attempting to actually access the services that I've registered I continue to get the following exception:
NOTICE: PHP message: Error: Class 'Drupal\MyNamespace\MyRegisteredService' not found in /var/www/html/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 259 #0 /var/www/html/web/core/lib/Drupal/Component/DependencyInjection/Container.php(173): Drupal\Component\DependencyInjection\Container->createService(Array, 'foo....')
I have registered the services correctly, and have set up dependency injection correctly (I believe) it's just accessing the services that is not working.
My file structure currently looks like:
- web
- modules
- custom
- foo
- foo.services.yml
- src
- MyService.php
- Plugin
- Block
- FooBlock.php
foo.services.yml looks like:
services:
foo.my_service:
class: Drupal\MyNamespace\MyRegisteredService
autowire: true
FooBlock.php looks like (simply for dependency injection):
namespace Drupal\foo\Plugin\Block;
use Drupal\MyNamespace\MyRegisteredService
use Drupal\Console\Bootstrap\Drupal;
use Drupal\Core\Block\BlockBase;
use GuzzleHttp\Client;
use Http\Client\Exception;
use phpDocumentor\Reflection\Types\Array_;
use stdClass;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FooBlock extends BlockBase implements ContainerFactoryPluginInterface {
private $my_service;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('foo.my_service'),
);
}
function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
Drupal\MyNamespace\MyRegisteredService $my_registered_service,
) {
$this->my_registered_service = $my_registered_service;
}
MyService.php looks like:
<?php
namespace Drupal\MyNamespace;
class MyService
{
static function say_hello()
{
return 'hello world';
}
}
I'm not sure if the way that I'm trying to set this up is incorrect or if I haven't set up dependency injection correctly. Some things that I have tried with no success is changing the namespace and removing Drupal\ from it but this changed little.
I've also tried following several guides (such as this) on how set up services, with little luck.
Any help would be appreciated.
I am not sure about the actual name of your service or namespace path, but I can see a few inconsistencies in your placeholder references:
Please change all references of Drupal\MyNamespace\MyRegisteredService to Drupal\foo\MyRegisteredService where foo is the name of your module. This should be lower cased.
Secondly Drupal\MyNamespace\MyRegisteredService references a class name MyRegisteredService whereas the example class name you give is MyService. These should be equal, so either go with one or the other.

How to get my own ICultureSelector implementation injected in the framework

I have implemented my version of ICultureSelector, in a custom module.
Here it is a part of its definition (my question is not about the logic to select the culture; I tried with my own namespace and also with a namespace same as the namespace used in the module Orchard.Localization):
namespace Orchard.Localization.Selectors
{
[OrchardFeature("Orchard.Localization.CultureSelector")]
public class ShortRouteCultureSelector : ICultureSelector
{
public CultureSelectorResult GetCulture(HttpContextBase context)
{
...
I put a breakpoint in the method GetCurrentCulture of the class CurrentCultureWorkContext in the Orchard.Framework project, and I see that the variable IEnumerable _cultureSelectors contains all the implementation of ICultureSelector of the module Orchard.Localization but not my implementation, that is never used.
What am I missing?
Remove the OrchardFeature attribute or define your own feature name as Orchard.Localization.CultureSelector is defined in Orchard.Localization module already.

ZfcUser extend User class with additional get function

I'm new to the Zend2 Framework and have installed ZfcUser with an added database column which I would like to access through:
<?php echo $this->zfcUserIdentity()->getOrg(); ?>
Any help on extending the User Class to access this variable would be greatly appreciated.
Ryan
Extend the ZfcUser user entity to include your new property and accessors. You'll need to do this in your own module, or if you're using the skeleton app, in the Application module will work.
<?php
namespace Application\Entity;
use ZfcUser\Entity\User;
class MyUser extends User
{
protected $org;
public function setOrg($org)
{
$this->org = $org;
return $this;
}
public function getOrg()
{
return $this->org;
}
}
Copy vendor/ZfcUser/config/zfcuser.global.php.dist to /config/autoload/zfcuser.global.php
Open the file you just copied in your editor, and find the section below
/**
* User Model Entity Class
*
* Name of Entity class to use. Useful for using your own entity class
* instead of the default one provided. Default is ZfcUser\Entity\User.
* The entity class should implement ZfcUser\Entity\UserInterface
*/
//'user_entity_class' => 'ZfcUser\Entity\User',
uncomment the line, and replace the value with the fully qualified class name of the MyUser entity you created
'user_entity_class' => 'Application\Entity\MyUser',
Then try accessing your method
<?php echo $this->zfcUserIdentity()->getOrg(); ?>

Monodroid Spinner Resource reference error

I'm following the spinner from monodroid tutorial. But encountered problem on the resource.
It cannot lookup the SimpleSpinnerItem & SimpleSpinnerDropDownItem on VS 2010.
Am I missing something?
Edit: Create a partial class to register android runtime as per jonp
public partial class Resource
{
public partial class Layout
{
[Register("simple_spinner_dropdown_item")]
public const int SimpleSpinnerDropDownItem = 17367049;
[Register("simple_spinner_item")]
public const int SimpleSpinnerItem = 17367048;
}
}
Edit 2: Tried the global resource
Edit 3: Conflict on my project namespace
I already identified why the const cannot be recognize. It's because of my namespace projectname.Android, it's being duplicated. When I changed it to projectname.AndroidMobile the global resource is there.
See the conflict below.
Also, to avoid the conflict just use the global:: as per jonp
You need to qualify the class, as there are two Resource types: one local to your project (Your.Namespace.Resource, located in Resource.designer.cs), and global::Android.Resource. You need to use global::Android.Resource.Layout.SimpleSpinnerItem.

Custom route class

In symfony 1.4 you could define a custom route class, where you override the generation of url with custom logic, for example:
custom:
class: sfDoctrineRouteCollection
options:
model: Custom
prefix_path: /custom/category/:category_id
column: id
route_class: CustomDoctrineRoute
class CustomDoctrineRoute extends sfDoctrineRoute
{
public function generate($params, $context = array(), $absolute = false)
{
if (!isset($params['category_id'])) {
$params['category_id'] = sfContext::getInstance()->getRequest()->getParameter('category_id');
}
return parent::generate($params, $context, $absolute);
}
}
This allows to write url_for('custom_show', array('id'=> $object['id'])) and not bother about context dependent parameters (category_id).
How do you approach this is symfony2?
I can think of 2 approaches to this. The first, and simplest, is to extend the Router class with your own and tell symfony to use your class in your parameters.yml or config.yml:
parameters:
router.class: Company\CoreBundle\Routing\MyCustomRouter
There's a more powerful (read: complicated) solution which allows you to define more dependencies on your router class by overriding or extending the whole router service. There is a bundle that does this called BeSimpleI18nRoutingBundle which you can look at to see how it's done.
Specifically, notice the CompilerPass where they replace the default router service with their own. You then have to implement the RouterInterface in your own router class. In this particular bundle they inject the original default router (after having renamed it in the compiler pass).

Resources