Laravel - "simulate"/call a route routine - php

In Laravel, I want to have dynamic routes for simple pages functionality.
So say I have couple routes, like /blog will call BlogController and so on. And I have routes like this /page/{slug} that call PageController.
How can I do it so if route is not found (for example /my-test-page), the system would call the controller as if the route was /page/my-test-page (so that 404 is only throw if PageController cannot find the page in the database).
I saw I can catch the 404 exceptions, but I don't know how I can simulate the route call to PageController from there?
Thanks a lot!

redirect(action(PageController#methodName))
Does this solves your problem? Of course you will need to import the redirect() method.

Related

Laravel getting 404 error when creating new route

it appears that when I created a new route, I receive the 404 error when trying to access the url, which is funny,. because all of my other routes are working just fine.
My web.php looks like so:
Auth::routes();
Route::post('follow/{user}', 'FollowsController#store');
Route::get('/acasa', 'HomeController#index')->name('acasa');
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Route::get('/profil/{user}/edit', 'ProfilesController#edit')->name('editareprofil');
Route::patch('/profil/{user}', 'ProfilesController#update')->name('updateprofil');
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Route::get('/alerte/url/{user}', 'UrlsController#index')->name('editurl');
Route::post('/alerte/url/{user}', 'UrlsController#store')->name('updateurl');
Route::get('/alerte/url/{del_id}/delete','UrlsController#destroy')->name('deleteurl');
The one that is NOT working when I am visiting http://127.0.0.1:8000/alerte is:
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The controller looks like so:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
class PaginaAlerte extends Controller
{
public function __construct() {
$this->middleware('auth');
}
public function index(User $user)
{
return view('alerte');
}
}
I am banging my head around as I cannot see which is the problem. It is not a live website yet, I am just developing on my Windows 10 pc using WAMP.
Moved my comment to a little bit explained answer.
So, in your route collection, you have two conflicting routes
Route::get('/{user}', 'ProfilesController#index')->name('profil');
and
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Imagine that Laravel is reading all routings from top to bottom and it stops to reading next one after the first match.
In your case, Laravel is thinking that alerte is a username and going to the ProfilesController#index controller. Then it tries to find a user with alerte username and returning 404 because for now, you don't have a user with this username.
So to fix 404 error and handle /alerte route, you just need to move the corresponding route before /{username} one.
But here is the dilemma that you got now. What if you will have a user with alerte username? In this case, the user can't see his profile page because now alerte is handling by another route.
And I'm suggesting to use a bit more friendly URL structure for your project. Like /user/{username} to handle some actions with users and still use /alerte to handle alert routes.
The following route catches the url /alerte as well
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Since this one is specified before
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The /alerte will go the the ProfilesController instead.
To fix this change the order of the url definitions or change either of the urls to have nesting e.g. /alerte/home or /user/{user}
Well.
Maybe this is too late, but I have all week dealing with this problem.
I made my own custom.php file and add it in the routes path of my Laravel project, and none of the routes were working at all.
This is how I solved it:
You must remember to edit the RouteServiceProvider.php file located in app\Providers path. In the map() function, you must add your .php file. That should work fine!
To avoid unexpected behaviors, map your custom routes first. Some Laravel based systems can "stop" processing routes if no one of the expected routes rules were satisfied. I face that problem, and was driving me crazy!
I would wish suggest to you declare your URL without the "/", like your first "post" route, because sometimes, I have been got this kind of errors (404).
So, my first recomendation is change the declaration of the route. After that, you should test your middleware, try without the construct, and try again.
Good luck!

How to add a new page to the admin view of laravel framework

I have a website in laravel framework and I am trying to add a simple new static page to the admin panel. I have done the following three steps:
Add a template to the views:
app/views/admin/MessageToAll.blade.php
Add the make view code in the controller.
public function MessageToAll(){
return View::make('admin.MessageToAll');
}
Added a route in app/routes.php
Route::get('/admin/MessageToAll',array('as'=>'MessageToAll','uses'=>'AdminController#MessageToAll'));
But when I go to to domain.com/admin/MessageToAll
it gives me a 404 page not found error. Does anyone know what have I missed as I think I have completed all steps for adding this view.
Just put your new route before the /admin/ route (to test it, you want to temporarily make it the very first route in routes.php). The problem is /admin/ or some other similar route executed before your new route.
Also, if you need to just execute static view, you can use something like this (works without using a controller):
Route::get('/admin/MessageToAll', function (){
return View::make('admin.MessageToAll');
});
in routes add:
Route::get('/admin/MessageToAll','yourController#yourMethod');

laravel routes, how to group similar in one file

I'm working with laravel(5.2), and there are a lot routes in my route file.
in fresh install I noticed that it was loading auth routes something like this.
Route::auth();
nothing else was there in routes.php file related to auth routes.
in my file, I've like this one
Route::get('color/event', 'ColorController#index');
Route::post('color/event', 'ColorController#post_message);
...
...
and many others, So I want to load all in laravel way, like Route::color(); and it should load all color related routes
Thanks for you time
you can try this
Route::resource('admin/settings','Admin\SettingsController');
and try this command
$ php artisan routes
Using Route::get(), Route::post() and similar functions is doing it the Laravel way - see the docs here https://laravel.com/docs/5.2/routing#basic-routing
Route::auth() is just a helper function introduced in Laravel 5.2 to keep all auth definitions together.
so, anyone if s/he is looking for same answer, I figured that out.
if you want something like Route::auth(); OR Route::color();//in my case or whatever you want to call it, you need to add custom function in your Router.php file. So solution will look like
//inside Router.php file
public function whatever(){
$this->get('app/', 'AppController#index');
$this->post('app/new', 'AppController#create');
}
and in your route.php file, you can do this.
Route::whatever();
But this is really dirty way to do that
so instead you can extend the base Router and register your router in bootstrap/app.php
$app->singleton('router', 'App\Your\Router');
so I community forces to use second approach.
for more details, have a look here.
Extending Router(laravel.io forum)
Extending default Laravel 5 Router
How to extend Router or Replace Customer Router class on Laravel5?
hope someone will find this useful
Thanks.

Laravel 5 Error Pages

I'm having problems creating error pages for my L5 app. I created a controller called BaseController which has all my CSS/JS and every other controller I have extends from it. How does one create an error 404 page which also extends BaseController?
Simply creating a view in views/errors/404.blade.php does not work since no styles are loaded. I'm using Twigbridge which is really useful when working with views.
Am assuming when you say controller has CSS/JS it means that your setting up layout using controllers.
Please note that this works for routes that you clearly define in your routes file and their respective controllers extend the Base.
For Error Pages they are not handle by your controllers. The error handlers are responsible for rendering their views and won't extend any layout. Hence you need to complete define it in your error view.
This should get you off on the right foot. Just use your standard extends syntax to extend your "default" layout.
Do a search for App::error, you will find the right spot.
App::error(function(Exception $exception, $code)
{
$params = array();
$request = Request::create('cms/noroute', 'GET', $params);
return Route::dispatch($request)->getContent();
});
There is no need to handle the errors within a controller or even the assets files. Imagine you have an error from laravel (missing a config file) so you will have the error before event the code could reach a controller. The same case goes for 404. If you do not have that route available, there is no need to let user reach a controller, this is handled by Exception Handler.
A good resource to learn more is Laravel documentation and this blog post

Some Routing problems in laravel 4

I am new to laravel 4 framework but was previously working on CI and CakePHP, i have some problems with routes in it (i may sound nerd, so bear with me.)
-> If i have 3 controller userController,adminController,editorController and many methods inside them, do i need to define routes for every methods inside it (ofcourse i am not using ResourceFull controller for them). Can't i have something by which the methods can be accessed by using the controllername followed by method name like we do in other frameWork.
E.g usersController have manageUser method, i wnt to access it like
http://localhost/project/users/manageUser
-> What is use of defining a route using Route::controller('users', 'UserController'); or restfull controller?
Thanks in advance :)
If you write
Route::controller('users', 'UserController')
runs the default function (index of all the objects), but you can write:
Route::get('/users', 'userController#function');
or
Route::post('/users', 'userController#function');
this route shows to Laravel what controller and function can call when you write this route, the diference is if you pass the parameters with get or post mode.
Hope I help you

Categories